note 57089 deleted from function.file-put-contents by aidan
| From: | aidan@php.net | Date: | Thu, 15 Dec 2005 12:57:17 +0000 |
| Subject: | note 57089 deleted from function.file-put-contents by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100191@lists.php.net to get a copy of this message | ||
Note Submitter: adam [NOSPAM AT] pilorz [NOSPAMDOT] net
----
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);
?>