note 105239 deleted from function.file-put-contents by danbrown

From: Date: Thu, 04 Aug 2011 15:05:56 +0000
Subject: note 105239 deleted from function.file-put-contents by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182220@lists.php.net to get a copy of this message
Note Submitter: DaveRandom ---- A comprehensive file_put_contents for PHP4. This supports everything documented above (as at 2011/08/04) except contexts, since they don't make sense in a PHP4 environment. Be aware that the same rules apply to using LOCK_EX here as apply to using it in flock() - it will block until a lock is aquired. <?php if (!function_exists('file_put_contents')) { // Define any missing constants foreach (array('FILE_USE_INCLUDE_PATH'=>1,'FILE_APPEND'=>8,'LOCK_EX'=>2,'LOCK_NB'=>4) as $constant => $value) if (!defined($constant)) define($constant,$value); function file_put_contents ($filename, $data, $flags = NULL) { // Parse flags $lock = $append = $include = FALSE; switch ($flags) { case FILE_USE_INCLUDE_PATH: $include = TRUE; break; case FILE_APPEND: $append = TRUE; break; case LOCK_EX: $lock = TRUE; break; case FILE_USE_INCLUDE_PATH | FILE_APPEND: $include = $append = TRUE; break; case FILE_USE_INCLUDE_PATH | LOCK_EX: $include = $lock = TRUE; break; case LOCK_EX | FILE_APPEND: $lock = $append = TRUE; break; case LOCK_EX | FILE_APPEND | FILE_USE_INCLUDE_PATH: $lock = $append = $include = TRUE; break; } $filename = str_replace('\\','/',$filename); // Use / as a separator as it works everywhere if ($include) // Check for file in include path foreach (explode(PATH_SEPARATOR,ini_get('include_path')) as $dirpath) if (is_file($filepath = rtrim(str_replace('\\','/',$dirpath),'/').'/'.ltrim($filename,'/'))) { $filename = $filepath; break; } // Error out if dir doesn't exist, can't open the file, or have been asked to lock and can't if (!is_dir(dirname($filename)) || (!$fp = @fopen($filename,($append) ? 'a' : 'w')) || ($lock && !flock($fp,LOCK_EX))) return FALSE; // Write data and return $written = fwrite($fp,(is_array($data)) ? implode('',$data) : $data); if ($lock) flock($fp,LOCK_UN); fclose($fp); return $written; } } ?>

« previous php.notes (#182220) next »