以下代码正在运行,但它不会更新它创建的文件的内容。
我可以看到文件内容已经改变(大小增加)但是当我从服务器下载文件时它是空的。 该文件是chmod到666及其父目录。
它是运行Apache和PHP的Linux服务器。 我也尝试使用fflush强制它冲洗内容。
<?php
header("Location: http://www.example.com");
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, '=');
fwrite($handle, $value);
fwrite($handle, '\r\n');
}
fwrite($handle, '\r\n');
fflush($handle);
fclose($handle);
?>
问题是什么?
谢谢!
答案 0 :(得分:0)
我认为一个好的做法是检查文件是否可以使用is_writable
写入,然后通过检查fopen
返回的值,通过代码是正确的方式来打开它。
试试这个:
$filename = "log.txt";
$mode = "a";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, $mode)) {
echo "Cannot open file ($filename)";
exit;
}
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, '=');
fwrite($handle, $value);
fwrite($handle, '\r\n');
}
fwrite($handle, '\r\n');
fflush($handle);
fclose($handle);
echo "Content written to file ($filename)";
} else {
echo "The file $filename is not writable";
}