note 75852 added to function.fwrite
| From: | ElliottBrueggeman at osu1 dot php dot net | Date: | Mon, 18 Jun 2007 20:24:17 +0000 |
| Subject: | note 75852 added to function.fwrite | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127728@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 12.3.53.162
----
Manual Page -- http://www.php.net/manual/en/function.fwrite.php
Edit -- https://master.php.net/note/edit/75852
Del: integrated -- https://master.php.net/note/delete/75852/integrated
Del: useless -- https://master.php.net/note/delete/75852/useless
Del: bad code -- https://master.php.net/note/delete/75852/bad+code
Del: spam -- https://master.php.net/note/delete/75852/spam
Del: non-english -- https://master.php.net/note/delete/75852/non-english
Del: in docs -- https://master.php.net/note/delete/75852/in+docs
Del: other reasons-- https://master.php.net/note/delete/75852
Reject -- https://master.php.net/note/reject/75852
Search -- https://master.php.net/manage/user-notes.php