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. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Aidan Lister <aidan@php.net>                                |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: file_put_contents.php,v 1.23 2005/01/26 04:55:13 aidan Exp $
  19.  
  20.  
  21. if (!defined('FILE_USE_INCLUDE_PATH')) {
  22.     define('FILE_USE_INCLUDE_PATH'1);
  23. }
  24.  
  25. if (!defined('FILE_APPEND')) {
  26.     define('FILE_APPEND'8);
  27. }
  28.  
  29.  
  30. /**
  31.  * Replace file_put_contents()
  32.  *
  33.  * @category    PHP
  34.  * @package     PHP_Compat
  35.  * @link        http://php.net/function.file_put_contents
  36.  * @author      Aidan Lister <aidan@php.net>
  37.  * @version     $Revision: 1.23 $
  38.  * @internal    resource_context is not supported
  39.  * @since       PHP 5
  40.  * @require     PHP 4.0.0 (user_error)
  41.  */
  42. if (!function_exists('file_put_contents')) {
  43.     function file_put_contents($filename$content$flags = null$resource_context = null)
  44.     {
  45.         // If $content is an array, convert it to a string
  46.         if (is_array($content)) {
  47.             $content implode(''$content);
  48.         }
  49.  
  50.         // If we don't have a string, throw an error
  51.         if (!is_scalar($content)) {
  52.             user_error('file_put_contents() The 2nd parameter should be either a string or an array',
  53.                 E_USER_WARNING);
  54.             return false;
  55.         }
  56.  
  57.         // Get the length of date to write
  58.         $length strlen($content);
  59.  
  60.         // Check what mode we are using
  61.         $mode ($flags FILE_APPEND?
  62.                     $mode 'a' :
  63.                     $mode 'w';
  64.  
  65.         // Check if we're using the include path
  66.         $use_inc_path ($flags FILE_USE_INCLUDE_PATH?
  67.                     true :
  68.                     false;
  69.  
  70.         // Open the file for writing
  71.         if (($fh @fopen($filename$mode$use_inc_path)) === false{
  72.             user_error('file_put_contents() failed to open stream: Permission denied',
  73.                 E_USER_WARNING);
  74.             return false;
  75.         }
  76.  
  77.         // Write to the file
  78.         $bytes = 0;
  79.         if (($bytes @fwrite($fh$content)) === false{
  80.             $errormsg sprintf('file_put_contents() Failed to write %d bytes to %s',
  81.                             $length,
  82.                             $filename);
  83.             user_error($errormsgE_USER_WARNING);
  84.             return false;
  85.         }
  86.  
  87.         // Close the handle
  88.         @fclose($fh);
  89.  
  90.         // Check all the data was written
  91.         if ($bytes != $length{
  92.             $errormsg sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
  93.                             $bytes,
  94.                             $length);
  95.             user_error($errormsgE_USER_WARNING);
  96.             return false;
  97.         }
  98.  
  99.         // Return length
  100.         return $bytes;
  101.     }
  102. }
  103.  
  104. ?>

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