0

Is there a reason why my code does not want to write to my text file:guestbook.txt?

<?php
//Create a variable called $file that holds the guestbook.txt
$file= "guestbook.txt";
//Create other variables
$name= $_REQUEST['name'];
$message= $_REQUEST['message'];
//Check to make sure that all fields are populated 
if(empty($name) || empty($message)) 
{
    echo "<center><h3>Sorry, all fields are required</h3><center>";
}
else 
{
 /*Where $fp is the file pointer, and fopen Opens file or URL, $file is the $file(message.txt) and "a" means 
 Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.*/
$fp = fopen($file,"a");
 //Use frwrite to write the write contents of string  to the file stream pointed to by handle $fp.
fwrite($fp, '<font size="3">
<br />
<br />
Name: '.$name.'<br />
<br />
<br />Message: '.$message.'<br />
');

 // Close the file pointed to by $fp.
fclose($fp);

echo '<font size="3">
<p align="center">Thank you '.$name.' for signing the Guestbook</p>
</font>'; 
}
?>
1
  • 1
    have you got the right permissions of the file? Are there any errors or notices displayed? Commented Nov 25, 2009 at 10:49

5 Answers 5

3

try turning error reporting on with

error_reporting(E_ALL);

to see any errors if you do not have the permissions to write you should get something like:

Warning: fopen(guestbook.txt): failed to open stream: Permission denied .....

Sign up to request clarification or add additional context in comments.

1 Comment

I just decided to use the database on my flash drive to store the info, thanks though for the heads up on error_reporting(E_ALL);
1

Works for me, a blind shot would be permissions to directory you are writing to are not set properly for the http/apache/nobody user.

Comments

0

Try specifying a path for the guestbook.txt file so you know where it will be and can be sure permissions are correct.

Comments

0

error_reporting(E_ALL) might not help you if you have disabled error reporting, you can try something like below to enable error reporting firsst:

ini_set('error_reporting', 'on'); error_reporting(E_ALL);

Comments

0

Should be either because, the user (apache user) doesn't have write permissions (or) the user doesn't have permission to create a file in that folder, if the file is not already existent.. Turning on error reporting will help in debugging..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.