我的代码:
function log_this($to_log, $prepend = null){
ob_start();
$fn = '../info.log';
$fp = fopen($fn, 'a');
fputs($fp, "'r'rnew log -------- 'r'r");
if(isset($prepend)) fputs($fp, $prepend . ":'r'r");
var_dump($to_log);
$content = ob_get_contents();
fputs($fp, $content);
fclose($fp);
ob_end_clean();
}
这是我在本地环境(MAMP)中经常使用的一个函数,用于记录wordpress中的内容。它总是有效的。现在它已经不起作用了。我试着理解了几天的原因,但找不到任何解决方案。我不是一个真正高级的php程序员,所以也许有一些事情我不知道,也应该。。有人能帮我吗?
顺便说一句,function_exists和file_exists.
我不确定fputs
为什么不工作,这可能与您的服务器文件夹权限有关(通常0755
或0775
是安全的),也可以添加一个条件来检查is_writable
以消除这种可能性。你试过使用file_get_contents
和file_put_contents
吗。
define('FILE_PATH', 'path/to/file/file.log');
function log_this($command, $array = null) {
//check if we can get to the file
if(file_exists(FILE_PATH)){
$current = file_get_contents(FILE_PATH);
$current .= $command;
if(!is_null($array)){
ob_start();print_r($array);$output = ob_get_clean();
$current .= $output;
}
file_put_contents(FILE_PATH, $current);
}
}