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.16 2004/06/12 06:53:00 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     $Revision: 1.16 $
  41.  * @internal    $resource_context is not supported
  42.  * @since       PHP 5
  43.  */
  44. if (!function_exists('file_put_contents'))
  45. {
  46.     function file_put_contents ($filename$content$flags = null$resource_context = null)
  47.     {
  48.         // If $content is an array, convert it to a string
  49.         if (is_array($content)) {
  50.             $content implode(''$content);
  51.         }
  52.  
  53.         // If we don't have a string, throw an error
  54.         if (!is_string($content)) {
  55.             trigger_error('file_put_contents() The 2nd parameter should be either a string or an array'E_USER_WARNING);
  56.             return false;
  57.         }        
  58.         
  59.         // Get the length of date to write
  60.         $length strlen($content);
  61.  
  62.         // Check what mode we are using
  63.         $mode ($flags FILE_APPEND?
  64.                     $mode 'a' :
  65.                     $mode 'w';
  66.  
  67.         // Check if we're using the include path
  68.         $use_inc_path ($flags FILE_USE_INCLUDE_PATH?
  69.                     true :
  70.                     false;
  71.  
  72.         // Open the file for writing
  73.         if (($fh @fopen($filename$mode$use_inc_path)) === false{
  74.             trigger_error('file_put_contents() failed to open stream: Permission denied'E_USER_WARNING);
  75.             return false;
  76.         }
  77.  
  78.         // Write to the file
  79.         $bytes = 0;
  80.         if (($bytes @fwrite($fh$content)) === false{
  81.             $errormsg sprintf('file_put_contents() Failed to write %d bytes to %s',
  82.                             $length,
  83.                             $filename);
  84.             trigger_error($errormsgE_USER_WARNING);
  85.             return false;
  86.         }
  87.  
  88.         // Close the handle
  89.         @fclose($fh);
  90.  
  91.         // Check all the data was written
  92.         if ($bytes != $length{
  93.             $errormsg sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
  94.                             $bytes,
  95.                             $length);
  96.             trigger_error($errormsgE_USER_WARNING);
  97.             return false;
  98.         }
  99.  
  100.         // Return length
  101.         return $bytes;
  102.     }
  103. }
  104.  
  105. ?>

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