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