note 49705 added to function.file-put-contents
| From: | codeslinger at compsalot dot com | Date: | Sat, 05 Feb 2005 19:47:09 +0000 |
| Subject: | note 49705 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84561@lists.php.net to get a copy of this message | ||
<?php
/*********************************
This is a substitue for file_put_contents which is only available on PHP5
We pass the data by ref to avoid memory duplication of possibly large values
per the spec, arrays are converted as join('', $array)
int file_put_contents ( string filename, mixed data [, int flags [, resource context]] )
Any functions which are conditionally defined must appear before they are used.
Caution: This does not do File Locking...
Tested on PHP 4.3.10; not tested and may not work on PHP 3
I have not tested the $Context, although fopen doesn't complain, it may not support it.
Written by codeslinger@compsalot.com 2/3/2005 Released to the public domain
*********************************/
// Define Flags for the function
// Note: These Values are arbitrary, I don't have access to PHP5 to see what they really are.
if (! defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 2);
if (! defined('FILE_APPEND')) define('FILE_APPEND', 4);
if (! function_exists('file_put_contents')) {
function file_put_contents($strFileName, &$MixedData, $Flags = 0, $Context = NULL ){
if (is_null($Context)){
$fp = fopen($strFileName, ($Flags & FILE_APPEND) ? "ab" :
"wb", $Flags & FILE_USE_INCLUDE_PATH );
} else {
$fp = fopen($strFileName, ($Flags & FILE_APPEND) ? "ab" :
"wb", $Flags & FILE_USE_INCLUDE_PATH, $Context );
}
if ($fp === FALSE){
//Error...
return(FALSE);
}
if (is_array($MixedData)){
$Result = fwrite($fp, join('', $MixedData));
} else {
$Result = fwrite($fp, $MixedData);
}
fclose($fp);
return($Result);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49705
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49705&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49705&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49705&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49705&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49705&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49705&report=yes
Search -- http://master.php.net/manage/user-notes.php