$fp = fopen('log.txt', 'w');
fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . '\n');上面的代码没有写入文件。$row['toolbar_id']是来自for each循环的值。有什么建议吗?没有PHP错误,因为文件确实会在我调试该部分时打开。
发布于 2011-09-28 12:37:51
试试这个额外的保证
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$fp = fopen('log.txt', 'ab');
if (false === $fp) {
throw new RuntimeException('Unable to open log file for writing');
}
$bytes = fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . PHP_EOL);
printf('Wrote %d bytes to %s', $bytes, realpath('log.txt'));
fclose($fp);编辑:将"write“标志(w)改为"append”(a),因为截断日志文件听起来不是个好主意
发布于 2012-09-19 14:44:06
https://bugs.php.net/bug.php?id=48607 fwrite和ftp有一个php bug,这意味着当fwrite之后直接调用fclose时,文件的最后一块有时不会被写入,在fclose修复这个问题之前,或者在你的情况下,在fclose之前登录到一个文件也可以停止它。
张贴以供将来参考!
发布于 2012-09-19 14:49:53
<?php
$filename = 'log.txt';
$somecontent = "Missing gallery image for: ";
if($row['toolbar_id'] != "")
{
$somecontent .= $row['toolbar_id'];
}
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
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";
}
?>试试这段代码。!!
https://stackoverflow.com/questions/7578350
复制相似问题