php - ob_start / fputs suddenly doesn't work anymore, is there anything that can stop it? [closed]

StackOverflow https://stackoverflow.com/questions/12234896

Вопрос

My code:

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();
}

It's a function I always use in my local environment (MAMP) to log things from wordpress. it always worked. Now it doesn't work anymore. I tried to understand why for a couple of days but can't find any solution. I'm not a really advanced php programmer, so maybe there's something I don't know and should.. Can anyone help me please?

By the way, function_exists and also file_exists, from where I call it.

Это было полезно?

Решение

I'm not sure why fputs isn't working, it is probably to do with your servers folder permissions (usually 0755 or 0775 is safe), could also add a condition to check is_writable to eliminate that possibility. Have you tried using file_get_contents and 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);
    }
 }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top