Writing to a file. Basic but I cant seem to get it to write!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Writing to a file. Basic but I cant seem to get it to write!

Post by Joe »

Recently I have been trying to create a small comment area where people who enter my site can enter comments and suggestions. I want the users name and their message to be written into a text file name comments.txt so others also have the ability to view it. Its very basic I know but it could help me a lot. Now the problem is when i press the submit button on the form it shows up saying that the file comments.txt is not writeable. The files are all in the right directorys aswell as the comments.txt file. Has anyone ever had a problem similar to this?. If so could you please help me!

My code for this project go's like:

<?php
$filename = "comments.txt";
$name = $_POST["username"];
$comments = $_POST["comments"];

if (is_writable($filename))
{

if (!$handle = fopen($filename, 'b'))
{
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $name, $comments) === FALSE)
{
echo "Error, Cannot write to file ($filename)";
exit;
}

echo "<b>Thank you, $name.</b><p> ($comments)";
fclose($handle);
}
else
{
echo "The file $filename is not writable.";
}
?>


Regards



Joe 8)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

'b' is not an option for fopen(). Try using 'a+'

fopen documentation:
http://ca3.php.net/manual/en/function.fopen.php
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Still not working!

Post by Joe »

Tried a+ and a with no luck at all. Any help appreciated!


Joe
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

w w+ ....

Any errors youre getting ?
Post Reply