pardicity3
03-21-2004, 04: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";
}
?>
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";
}
?>