Ever want to use [man]file_put_contents/man in PHP 4? It's a very helpful function. Well, I've made this to create that function in PHP4. The only difference is that this function doesn't support resource context.
If you find any changes/improvements please let me know. Thanks
// Check to see if functin exists
if (!function_exists('file_put_contents')) {
// Define constants used by function, if not defined
if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
if (!defined('FILE_APPEND')) define('FILE_APPEND', 8);
// Define function and arguments
function file_put_contents($file, &$data, $flags=0)
{
// Varify arguments are correct types
if (!is_string($file)) return(false);
if (!is_string($data) && !is_array($data)) return(false);
if (!is_int($flags)) return(false);
// Set the include path and mode for fopen
$include = false;
$mode = 'wb';
// If data in array type..
if (is_array($data)) {
// Make sure it's not multi-dimensional
reset($data);
while (list(, $value) = each($data)) {
if (is_array($value)) return(false);
}
unset($value);
reset($data);
// Join the contents
$data = implode('', $data);
}
// Check for flags..
// If include path flag givin, set include path
if ($flags&FILE_USE_INCLUDE_PATH) $include = true;
// If append flag givin, set append mode
if ($flags&FILE_APPEND) $mode = 'ab';
// Open the file with givin options
if (!$handle = @fopen($file, $mode, $include)) return(false);
// Write data to file
if (($bytes = fwrite($handle, $data)) === false) return(false);
// Close file
fclose($handle);
// Return number of bytes written
return($bytes);
}
}