note 75852 deleted from function.fwrite by tularis

From: Date: Thu, 18 Oct 2007 15:49:07 +0000
Subject: note 75852 deleted from function.fwrite by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-131508@lists.php.net to get a copy of this message
Note Submitter: Elliott Brueggeman ---- This is an extension / reply to the previous posting by chaobreederxl at gmail dot com: Creating a text file (.txt) would probably be more useful in the end than an html file. In addition to being harder to parse using PHP than the .txt file, the html would not be formed properly (missing <head> tags, opening and closing <html> tags, etc.) and may not actually display rendered html code in some browsers. To create a text file that is compatible with both unix and windows platforms, call the fopen and fwrite functions like this: $today = date("Y_M_j"); $logfile = $today."_log.txt"; $dir = 'logs'; $saveLocation=$dir . '/' . $logfile; if(!is_dir($dir)) { mkdir($dir,"0755"); } if (!$handle = fopen($saveLocation, "a")) { //ERROR, DO SOMETHING exit; } else { if(fwrite($handle,$logMessage.'\r\n')===FALSE) { //ERROR, DO SOMETHING exit; } fclose($handle); } I did some minor error handling here just to show how that could be setup. I also added the "\r\n" newline chars. The \r part is specifically meant for windows compatibility, but does not display improperly in unix based systems. I also only used the "a" option on fopen, because "a+" is meant for both reading and writing, and we are only going to be writing in this example. I have a description of a script similar to this, with a little more detail, at http://www.ebrueggeman.com/php_site_access_log.php if interested. Hope this helps ~ Elliott

« previous php.notes (#131508) next »