PHP / Apache fwrite更新修改日期但不写入



使用PHP编写简单的示例,代码更新文件(修改日期/时间更改),但没有写入任何内容。没有错误。我:

Success, write (Add this to the file) to file (./errors/test.txt)

然而这个文件是空的,日期修改了——就好像它被写到了。

我在PHP 7.0/Apache2 Debian(树莓派B)上。我开始将文件权限设置为755,然后将其更改为777,没有成功。Owner是www-data -我试过root和另一个Owner -都失败了,因为你需要将Owner设置为www-data才能写入。

我已经尝试了./errors/test.txt错误/用法-没有区别。

我试过fopen ($ filename ' ')andfopen($filename, 'w')并且两者产生相同的结果。

<?php
$filename = './errors/test.txt';
$somecontent = "Add this to the filen";
// 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, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>

添加长度fwriteFunction for me.

用这个替换写代码。

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent,strlen($somecontent)) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

最新更新