note 49760 deleted from function.file-put-contents by aidan

From: Date: Mon, 14 Feb 2005 05:16:02 +0000
Subject: note 49760 deleted from function.file-put-contents by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-84990@lists.php.net to get a copy of this message
Note Submitter: rpanning at hotmail dot com ---- A PHP 3.0.7+ & 4 version of file_put_contents(). The only difference is this function doesn't support resource context. I think this should support PHP 3 but I have not tested this, if someone does please let me know at the support forum. Support for this function can be found at the PHP Builder site: http://www.phpbuilder.com/board/showthread.php?s=&threadid=10292234 Just a note for codeslinger's function, the constant values are 1 and 8, and resource context support for fopen() wasn't added until PHP 5 anyway, so it won't work. <?php // 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); } } ?>

« previous php.notes (#84990) next »