How to use fopen() in php? -


i trying make code able log kind of errors on opening database. trying , create .txt file logs saved in there in case of error.

what ve done far :

bla bla bla } catch(pdoexception $e) {         //in case of error , create log file error         //$pdoexception_file = 'pdoexception_file_' . date('y_m_d-h-i-s') . '.txt' ;         $pdoexception_file =  'pdoexception_file' . '.txt' ;         $fh = fopen($pdoexception_file, 'w') or die();         fwrite($fh, date('y_m_d-h-i-s') . ' pdoexception error: ' . $e->getmessage() . "\n\n" );         fclose($fh);         echo 'error: ' . $e->getmessage();          } 

what expected code wrote create 1 .txt file "pdoexception" file , inside logs :

2013_05_02-12-40-02 pdoexception error: bla bla bla   2013_05_02-12-43-02 pdoexception error: bla bla bla    2013_05_02-13-45-02 pdoexception error: bla bla bla 

that means every time have error , open file , write error make 2 new lines close file. next time error , write end of file error etc etc.

what happens though rewrite in beginning of file on old data. how can avoid that?

in code have:

$fh = fopen($pdoexception_file, 'w') or die() 

you should use:

$fh = fopen($pdoexception_file, 'a') or die() 

the w open's file writing , places file pointer @ beginning of file , truncate file 0 length (deleting contents). if file not exist, attempt create it. a open file writing , place file pointer @ end of file. if file not exist attempt create it.

you can find more information on fopen() here: http://php.net/manual/en/function.fopen.php


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -