note 57089 added to function.file-put-contents
| From: | adam | Date: | Fri, 23 Sep 2005 08:40:13 +0000 |
| Subject: | note 57089 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95651@lists.php.net to get a copy of this message | ||
Just another implementation of this function in PHP<5:
<?
define('FILE_APPEND', 1);
// define('LOCK_EX', 2); - constant defined in PHP, is 2 as well
function file_put_contents($filename, $data, $flags = 0, $f = FALSE)
{
if(($f===FALSE) && (($flags%2)==1)) $f=fopen($filename, 'a'); else
if($f===FALSE) $f=fopen($filename, 'w');
if(round($flags/2)==1) while(!flock($f, LOCK_EX)) { /* lock */ }
if(is_array($data)) $data=implode('', $data);
fwrite($f, $data);
if(round($flags/2)==1) flock($f, LOCK_UN);
fclose($f);
}
?>
I don't know does this function works the same as original, but it works fine ;). For example:
<?
//It works with flags:
file_put_contents('foo.txt', 'foo foo foo and just another foo
;)'."\n".'Lorem ipsum...', LOCK_EX+FILE_APPEND);
//... with Arrays:
file_put_contents('fooarray.txt', Array('foo foo foo and just another foo ;)',
'Lorem ipsum...'));
//... and alredy existing file resources:
$f=fopen('foo.txt', 'w');
file_put_contents('', 'Lorem ipsum...', LOCK_EX, $f);
?>
----
Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57089
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57089&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57089&report=yes
Search -- http://master.php.net/manage/user-notes.php