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. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Aidan Lister <aidan@php.net>                                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: file_put_contents.php,v 1.14 2004/05/29 08:54:44 aidan Exp $
  20. //
  21.  
  22.  
  23. if (!defined('FILE_APPEND')) {
  24.     define('FILE_APPEND'8);
  25. }
  26.  
  27. if (!defined('FILE_USE_INCLUDE_PATH')) {
  28.     define('FILE_USE_INCLUDE_PATH'1);
  29. }
  30.  
  31. /**
  32.  * Replace file_put_contents()
  33.  *
  34.  * Added in PHP 5
  35.  *
  36.  * @category    PHP
  37.  * @package     PHP_Compat
  38.  * @link        http://php.net/function.file_put_contents
  39.  * @author      Aidan Lister <aidan@php.net>
  40.  * @version     1.0
  41.  * @internal    $resource_context is not supported
  42.  */
  43. if (!function_exists('file_put_contents'))
  44. {
  45.     function file_put_contents ($filename$content$flags = null$resource_context = null)
  46.     {
  47.         // If $content is an array, convert it to a string
  48.         if (is_array($content)) {
  49.             $content implode(''$content);
  50.         }
  51.  
  52.         // If we don't have a string, throw an error
  53.         if (!is_string($content)) {
  54.             trigger_error('file_put_contents() The 2nd parameter should be either a string or an array'E_USER_WARNING);
  55.             return false;
  56.         }        
  57.         
  58.         // Get the length of date to write
  59.         $length strlen($content);
  60.  
  61.         // Check what mode we are using
  62.         $mode ($flags FILE_APPEND?
  63.                     $mode 'a' :
  64.                     $mode 'w';
  65.  
  66.         // Check if we're using the include path
  67.         $use_inc_path ($flags FILE_USE_INCLUDE_PATH?
  68.                     true :
  69.                     false;
  70.  
  71.         // Open the file for writing
  72.         if (($fh @fopen($filename$mode$use_inc_path)) === false{
  73.             trigger_error('file_put_contents() failed to open stream: Permission denied'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.             trigger_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.             trigger_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 10:17:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.