ob_start/fputs突然没有了';我再也不工作了,有什么能阻止它的吗


ob_start / fputs suddenly doesn't work anymore, is there anything that can stop it?

我的代码:

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为什么不工作,这可能与您的服务器文件夹权限有关(通常07550775是安全的),也可以添加一个条件来检查is_writable以消除这种可能性。你试过使用file_get_contentsfile_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);
    }
 }