【问题标题】:php - ob_start / fputs suddenly doesn't work anymore, is there anything that can stop it? [closed]php - ob_start / fputs 突然不再工作了,有什么可以阻止它的吗? [关闭]
【发布时间】:2012-09-02 09:40:59
【问题描述】:

我的代码:

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,我称之为它。

【问题讨论】:

  • 您说的是“它不再起作用”,这完全没有任何意义。你有错误吗?您是否没有将var_dump() 写入文件,或者您没有写入nothing(即甚至没有new log --------)?
  • 你确定log_this函数被调用了吗?您可以在函数中记录error_log(__METHOD__); 之类的内容并检查您是否在 apache 错误日志中获得了价值?
  • @lanzz:正如我所写,该函数的目的是记录事物。所以我的意思是它不再记录。即使,正如我所写,function_exists 和 file_exists。 info.log 保持空白。

标签: php ob-start fputs ob-get-contents


【解决方案1】:

我不确定为什么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);
    }
 }

【讨论】:

  • 谢谢你,我会尝试并最终发布结果!
  • 好吧……我忘了我有不止一个 info.log 文件,并且该函数没有抛出任何错误,因为它实际上是在写一个真实的文件,而不是我的那个调查。嗯...冲洗这里..:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 2014-04-16
  • 2012-06-11
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多