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.20 2004/08/19 10:09:53 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.20 $
  38.  * @internal    $resource_context is not supported
  39.  * @since       PHP 5
  40.  * @require     PHP 4.0.1 (trigger_error)
  41.  */
  42. if (!function_exists('file_put_contents'))
  43. {
  44.     function file_put_contents($filename$content$flags = null$resource_context = null)
  45.     {
  46.         // If $content is an array, convert it to a string
  47.         if (is_array($content)) {
  48.             $content implode(''$content);
  49.         }
  50.  
  51.         // If we don't have a string, throw an error
  52.         if (!is_string($content)) {
  53.             trigger_error('file_put_contents() The 2nd parameter should be either a string or an array'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.             trigger_error('file_put_contents() failed to open stream: Permission denied'E_USER_WARNING);
  73.             return false;
  74.         }
  75.  
  76.         // Write to the file
  77.         $bytes = 0;
  78.         if (($bytes @fwrite($fh$content)) === false{
  79.             $errormsg sprintf('file_put_contents() Failed to write %d bytes to %s',
  80.                             $length,
  81.                             $filename);
  82.             trigger_error($errormsgE_USER_WARNING);
  83.             return false;
  84.         }
  85.  
  86.         // Close the handle
  87.         @fclose($fh);
  88.  
  89.         // Check all the data was written
  90.         if ($bytes != $length{
  91.             $errormsg sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
  92.                             $bytes,
  93.                             $length);
  94.             trigger_error($errormsgE_USER_WARNING);
  95.             return false;
  96.         }
  97.  
  98.         // Return length
  99.         return $bytes;
  100.     }
  101. }
  102.  
  103. ?>

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