PHP_Compat
[ class tree: PHP_Compat ] [ index: PHP_Compat ] [ all elements ]

Source for file file_put_contents.php

Documentation is available at file_put_contents.php

  1. <?php
  2. if (!defined('FILE_USE_INCLUDE_PATH')) {
  3.     define('FILE_USE_INCLUDE_PATH'1);
  4. }
  5.  
  6. if (!defined('LOCK_EX')) {
  7.     define('LOCK_EX'2);
  8. }
  9.  
  10. if (!defined('FILE_APPEND')) {
  11.     define('FILE_APPEND'8);
  12. }
  13.  
  14.  
  15. /**
  16.  * Replace file_put_contents()
  17.  *
  18.  * @category    PHP
  19.  * @package     PHP_Compat
  20.  * @license     LGPL - http://www.gnu.org/licenses/lgpl.html
  21.  * @copyright   2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
  22.  * @link        http://php.net/function.file_put_contents
  23.  * @author      Aidan Lister <aidan@php.net>
  24.  * @version     $Revision: 269597 $
  25.  * @internal    resource_context is not supported
  26.  * @since       PHP 5
  27.  * @require     PHP 4.0.0 (user_error)
  28.  */
  29. function php_compat_file_put_contents($filename$content$flags = null$resource_context = null)
  30. {
  31.     // If $content is an array, convert it to a string
  32.     if (is_array($content)) {
  33.         $content implode(''$content);
  34.     }
  35.  
  36.     // If we don't have a string, throw an error
  37.     if (!is_scalar($content)) {
  38.         user_error('file_put_contents() The 2nd parameter should be either a string or an array',
  39.             E_USER_WARNING);
  40.         return false;
  41.     }
  42.  
  43.     // Get the length of data to write
  44.     $length strlen($content);
  45.  
  46.     // Check what mode we are using
  47.     $mode ($flags FILE_APPEND?
  48.                 'a' :
  49.                 'wb';
  50.  
  51.     // Check if we're using the include path
  52.     $use_inc_path ($flags FILE_USE_INCLUDE_PATH?
  53.                 true :
  54.                 false;
  55.  
  56.     // Open the file for writing
  57.     if (($fh @fopen($filename$mode$use_inc_path)) === false{
  58.         user_error('file_put_contents() failed to open stream: Permission denied',
  59.             E_USER_WARNING);
  60.         return false;
  61.     }
  62.  
  63.     // Attempt to get an exclusive lock
  64.     $use_lock ($flags LOCK_EX? true : false ;
  65.     if ($use_lock === true{
  66.         if (!flock($fhLOCK_EX)) {
  67.             return false;
  68.         }
  69.     }
  70.  
  71.     // Write to the file
  72.     $bytes = 0;
  73.     if (($bytes @fwrite($fh$content)) === false{
  74.         $errormsg sprintf('file_put_contents() Failed to write %d bytes to %s',
  75.                         $length,
  76.                         $filename);
  77.         user_error($errormsgE_USER_WARNING);
  78.         return false;
  79.     }
  80.  
  81.     // Close the handle
  82.     @fclose($fh);
  83.  
  84.     // Check all the data was written
  85.     if ($bytes != $length{
  86.         $errormsg sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
  87.                         $bytes,
  88.                         $length);
  89.         user_error($errormsgE_USER_WARNING);
  90.         return false;
  91.     }
  92.  
  93.     // Return length
  94.     return $bytes;
  95. }
  96.  
  97.  
  98. // Define
  99. if (!function_exists('file_put_contents')) {
  100.     function file_put_contents($filename$content$flags = null$resource_context = null)
  101.     {
  102.         return php_compat_file_put_contents($filename$content$flags$resource_context);
  103.     }
  104. }

Documentation generated on Mon, 11 Mar 2019 15:39:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.