...

View Full Version : fwrite issue with writable file



pardicity3
03-21-2004, 05:38 AM
My issues always seem so trivial... oh well.

I am currentyl writing a script that retrieves a user's email address and adds it to a text file. It's a rather simple script that allows users to sign up for a newsletter. I would use a database, but the university doesn't provide me with one...

Anyway, I know all my code works up until the fwrite() command. I'm not sure exactly what is happening, but the script always says it "could not write to file" (as per the die() command). Here's the code:
<?php

$email = $_GET['email'];
$filename = "emails.txt";

if(eregi("^[a-z0-9\._-]+".
"@{1}"."([a-z0-9]{1}[a-z0-9-]*[a-z0-9]{1}\.{1})+".
"([a-z]+\.){0,1}".
"([a-z]+){1}$", $email)) {

$handle = fopen($filename, "a+");
$contents = fread($handle, filesize($filename));
$emails = explode("; ", $contents);
$count = count($emails);

for($i=0;$i<$count; $i++) {
if($email == $emails[$i]) {
exit("email already exists");
}
}

$femail = $email . "; ";
echo(is_writable($filename)) ? "is writable" : "is not writable"; //just to test
fwrite($handle, $femail) or die("could not write to file");
}
else {
echo "already there";
}

?>

sidney
03-21-2004, 04:20 PM
is the file your writing to have the right permissions

ie writable by group chmod 666 on unix

a mystery? on windows

pardicity3
03-21-2004, 11:33 PM
It is on a windows machine, and the script doesn't even work if the script itself creates the file (it will work the first time, but doesn't work once the file is already created). Also, the is_writable() command returns true, which makes me believe the file is writable...

firepages
03-22-2004, 03:35 AM
fwrite() on win32 requires a binary flag ...


<?php
$fp = fopen( $file, 'ab' ) ;
?>


or just use fputs() instead of fwrite() .

pardicity3
03-22-2004, 08:47 PM
Adding the binary flag makes the script work locally, but when I upload it to my personal web space (provided by the university) the script stops working. I added some if else statements to act as little error checkers, and it seems that neither fopen() nor fwrite() works. I noticed fopen() wasn't working when a my specified file wasn't created despite it not existing. This may be a server issue, I don't know. The university's server doesn't have the mod_rewrite module installed on apache, so who knows what else they won't allow us to do.

Here is the code so far:
<?php

$email = $_GET['email'];
$filename = "emails.txt";
chmod($filename, 0777); //just to try and chmod it...

if(eregi("^[a-z0-9\._-]+".
"@{1}"."([a-z0-9]{1}[a-z0-9-]*[a-z0-9]{1}\.{1})+".
"([a-z]+\.){0,1}".
"([a-z]+){1}$", $email)) {

if($handle = fopen($filename, "a+")) {
echo "opened\n";
}
else {
echo "not opened\n";
}
$contents = fread($handle, filesize($filename));
$emails = explode("; ", $contents);
$count = count($emails);

for($i=0;$i<$count; $i++) {
if($email == $emails[$i]) {
header("Location: http:\/\/" . $_SERVER['HTTP_HOST'] . "/~mking5/?e=twice");
exit;
}
}

$femail = $email . "; ";
if(is_writable($filename)) {
echo "is writable\n";
}
else {
echo "not writable\n";
}
if(fwrite($handle, $femail)) {
header("Location: http:\/\/" . $_SERVER['HTTP_HOST'] . "/~mking5/?e=entered");
exit;
}
}
else {
header("Location: http:\/\/" . $_SERVER['HTTP_HOST'] . "/~mking5/?e=invalid");
}

?>If you want to try the script, you can go to http://www3.nd.edu/~mking5/ and then enter an email in the newsletter form. You should see the script output both "not opened" and "not writable".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum