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