开发者

php code will not write to text file

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'];
//C开发者_如何学运维heck 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>'; 
}
?>


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 .....


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.


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


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);


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜