note 105239 added to function.file-put-contents
| From: | DaveRandom at osu1 dot php dot net | Date: | Thu, 04 Aug 2011 14:50:23 +0000 |
| Subject: | note 105239 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182214@lists.php.net to get a copy of this message | ||
A comprehensive file_put_contents for PHP4. This supports everything documented above (as at
2011/08/04) except contexts, since they don't make sense in a PHP4 environment.
Be aware that the same rules apply to using LOCK_EX here as apply to using it in flock() - it will
block until a lock is aquired.
<?php
if (!function_exists('file_put_contents')) {
// Define any missing constants
foreach
(array('FILE_USE_INCLUDE_PATH'=>1,'FILE_APPEND'=>8,'LOCK_EX'=>2,'LOCK_NB'=>4)
as $constant => $value)
if (!defined($constant)) define($constant,$value);
function file_put_contents ($filename, $data, $flags = NULL) {
// Parse flags
$lock = $append = $include = FALSE;
switch ($flags) {
case FILE_USE_INCLUDE_PATH:
$include = TRUE;
break;
case FILE_APPEND:
$append = TRUE;
break;
case LOCK_EX:
$lock = TRUE;
break;
case FILE_USE_INCLUDE_PATH | FILE_APPEND:
$include = $append = TRUE;
break;
case FILE_USE_INCLUDE_PATH | LOCK_EX:
$include = $lock = TRUE;
break;
case LOCK_EX | FILE_APPEND:
$lock = $append = TRUE;
break;
case LOCK_EX | FILE_APPEND | FILE_USE_INCLUDE_PATH:
$lock = $append = $include = TRUE;
break;
}
$filename = str_replace('\\','/',$filename); // Use / as a separator as it
works everywhere
if ($include)
// Check for file in include path
foreach (explode(PATH_SEPARATOR,ini_get('include_path')) as $dirpath)
if (is_file($filepath =
rtrim(str_replace('\\','/',$dirpath),'/').'/'.ltrim($filename,'/')))
{
$filename = $filepath;
break;
}
// Error out if dir doesn't exist, can't open the file, or have been asked to lock
and can't
if (!is_dir(dirname($filename)) || (!$fp = @fopen($filename,($append) ? 'a' :
'w')) || ($lock && !flock($fp,LOCK_EX))) return FALSE;
// Write data and return
$written = fwrite($fp,(is_array($data)) ? implode('',$data) : $data);
if ($lock) flock($fp,LOCK_UN);
fclose($fp);
return $written;
}
}
?>
----
Server IP: 92.48.74.199
Probable Submitter: 77.107.172.13
----
Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php
Edit -- https://master.php.net/note/edit/105239
Del: integrated -- https://master.php.net/note/delete/105239/integrated
Del: useless -- https://master.php.net/note/delete/105239/useless
Del: bad code -- https://master.php.net/note/delete/105239/bad+code
Del: spam -- https://master.php.net/note/delete/105239/spam
Del: non-english -- https://master.php.net/note/delete/105239/non-english
Del: in docs -- https://master.php.net/note/delete/105239/in+docs
Del: other reasons-- https://master.php.net/note/delete/105239
Reject -- https://master.php.net/note/reject/105239
Search -- https://master.php.net/manage/user-notes.php