fwrite not writing

19,944

Try this for extra surety

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

Edit: Changed the "write" flag (w) to "append" (a) as truncating a log file doesn't sound like a great idea

Share:
19,944

Related videos on Youtube

lockdown
Author by

lockdown

Updated on June 13, 2022

Comments

  • lockdown
    lockdown almost 2 years
     $fp = fopen('log.txt', 'w');
     fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . '\n');
    

    The code above is not writing to the file. the $row['toolbar_id'] is a value from a for each loop. Any suggestions? There is no PHP error, as the file does open as I have debugged that part.

  • lockdown
    lockdown over 12 years
    I have tried this as well, No error, and still no writing to the file. I put permissions on the .txt file as 777 just for sake of not missing anything.
  • Phil
    Phil over 12 years
    @BrandonEllis I've added a debugging message for the fwrite() call. Try that and see what it says. I have a feeling you may be looking at the wrong log.txt file.
  • Phil
    Phil over 12 years
    @BrandonEllis So what was the problem?
  • lockdown
    lockdown over 12 years
    I think you were right in terms of which log-file it was writing to. Although, there were no other log files created and with no fopen error its still rather confusing as to what was happening.
  • MoshMage
    MoshMage almost 12 years
    I know this is from last year but.. what does the 'ab' mean? a=append and the b option stands for what?
  • Phil
    Phil almost 12 years
    @MoshMage b == binary mode. From the docs - For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
  • MoshMage
    MoshMage almost 12 years
    Thanks for the reply; somehow my code wouldn't work without the 'b' hence my asking :)