note 49760 added to function.file-put-contents

From: Date: Tue, 08 Feb 2005 06:49:28 +0000
Subject: note 49760 added to function.file-put-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-84645@lists.php.net to get a copy of this message
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); } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49760 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49760&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49760&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49760&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49760&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49760&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49760&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84645) next »