php代码不会写入文本文件

时间:2009-11-25 10:44:02

标签: php

我的代码是否有理由不想写入我的文本文件: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>'; 
}
?>

5 个答案:

答案 0 :(得分:3)

尝试使用

打开错误报告
error_reporting(E_ALL);

如果您没有写入权限,可以看到任何错误:

警告:fopen(guestbook.txt):无法打开流:权限被拒绝.....

答案 1 :(得分:1)

对我来说,对于http / apache / nobody用户来说,无法正确设置目录的权限。

答案 2 :(得分:0)

尝试为guestbook.txt文件指定路径,以便知道它的位置,并确保权限正确。

答案 3 :(得分:0)

如果您已禁用错误报告,

error_reporting(E_ALL)可能无法帮助您,您可以尝试以下方式启用错误报告第一次:

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

答案 4 :(得分:0)

应该是因为,用户(apache用户)没有写入权限(或)用户没有权限在该文件夹中创建文件,如果该文件尚不存在。启用错误报告将有助于调试..