note 47232 added to function.file-put-contents
| From: | PEAR::Package::PHP_Compat | Date: | Sun, 07 Nov 2004 20:07:05 +0000 |
| Subject: | note 47232 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80236@lists.php.net to get a copy of this message | ||
Use this function from PEAR::Package::PHP_Compat for PHP>4.0.1
<?php // require PHP 4.0.1 (trigger_error)
if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
if (!defined('FILE_APPEND')) define('FILE_APPEND', 8);
if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $content, $flags = null, $resource_context = null) {
// If $content is an array, convert it to a string
if (is_array($content)) $content = implode('', $content);
// If we don't have a string, throw an error
if (!is_string($content)) {
trigger_error('file_put_contents() The 2nd parameter should be either a string or
an array', E_USER_WARNING);
return false;
}
// Get the length of date to write
$length = strlen($content);
// Check what mode we are using
$mode = ($flags & FILE_APPEND) ? 'a' : 'w';
// Check if we're using the include path
$use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ? true : false;
// Open the file for writing
if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) {
trigger_error('file_put_contents() failed to open stream: Permission denied',
E_USER_WARNING);
return false;
}
// Write to the file
if (($bytes = @fwrite($fh, $content)) === false) {
$errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s',
$length, $filename);
trigger_error($errormsg, E_USER_WARNING);
return false;
}
// Close the handle
@fclose($fh);
// Check all the data was written
if ($bytes != $length) {
$errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out
of free disk space.', $bytes, $length);
trigger_error($errormsg, E_USER_WARNING);
return false;
}
// Return length
return $bytes;
} // end function
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47232
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47232&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47232&report=yes
Search -- http://master.php.net/manage/user-notes.php