Does anyone have a good working example of how to perform file_put_contents() equivalent for PHP 4+? Upgrading to PHP 5+ is absolutely, positively, unequivocably never going to happen at this environment..
So what do I do? I wrote my own version, but if the file does not [yet] exist, it bombs. Every online example I find is the same way, yet isn't file_put_contents() supposed to create the file if it does not yet exist? If so, how do I implement it?
Here's what I have so far:
if (!function_exists('file_put_contents')) {
/**
* Place content into file
*
* *NOTE* This function will also utilize is_binary() to determine if the file is to be binary and will handle accordingly
*
* @access public
* @param string $filePath
* @param mixed $data
* @param int $flags (optional)
* @return int Returns {@link http://us2.php.net/manual/en/function.filesize.php filesize()} of file or returns false to mirror behavior of {@link http://us2.php.net/manual/en/function.file-put-contents.php file_put_contents()} in PHP 5+
* @link http://us2.php.net/manual/en/function.file-put-contents.php Click here for information on file_put_contents()
* @see is_binary
* @see actual_path
*/
function file_put_contents($filePath, $data, $flags = '') {
$flags = (FLAG_APPEND || 'a' || 'A' || 'a+' || 'A+') ? 'a+' : 'w+';
if (is_binary($filePath)) $flags = preg_replace('/^([^\+])+\+$/', '$1b+', $flags); // BINARY APPEND OR WRITE
$fileID = @fopen(actual_path($filePath), $flags);
if (!$fileID) trigger_error("Unable to open \"$filePath\" for writing: ", E_USER_ERROR);
if ($fileID) {
@fputs($fileID, $data);
@fclose($fileID);
}
// TO MIRROR BEHAVIOR IN file_put_contents() IN PHP 5+
if (@is_file(actual_path($filePath)) && @filesize(actual_path($filePath)) > 0) return @filesize(actual_path($filePath)); else return false;
}
}