file_put_contents

(PHP 5)

file_put_contents -- 将一个字符串写入文件

说明

int file_put_contents ( string filename, string data [, int flags [, resource context]] )

和依次调用 fopen()fwrite() 以及 fclose() 功能一样。

参数 data 可以是数组(但不能为多元数组),这就相当于 file_put_contents($filename, join('', $array))

自 PHP 5.1.0 起,data 参数也可以被指定为 stream 资源,这里 stream 中所保存的缓存数据将被写入到指定文件中,这种用法就相似于使用 stream_copy_to_stream() 函数。

参数

filename

要被写入数据的文件名

data

要写入的数据。类型可以是 stringarray 或者是 stream 资源(如上面所说的那样).

flags

flags 可以是 FILE_USE_INCLUDE_PATH, FILE_APPEND 和/或 LOCK_EX (获得一个独占锁定),然而使用 FILE_USE_INCLUDE_PATH 时要特别谨慎。

context

一个 context 资源

返回值

该函数将返回写入到文件内数据的字节数

更新日志

版本说明
5.1.0 添加了对 LOCK_EX 的支持和 data 参数处理 stream 资源的功能。

注释

注: 本函数可安全用于二进制对象。

注: 对 context 的支持是 PHP 5.0.0 添加的。有关 context 的说明请参考参考 CXLI, Stream Functions

提示: 如果“fopen wrappers”已经被激活,则在本函数中可以把 URL 作为文件名来使用。请参阅 fopen() 函数来获取怎样指定文件名的详细信息以及支持 URL 封装协议的列表:附录 L


add a note add a note User Contributed Notes
egingell at sisna dot com
23-Jul-2006 07:11
In reply to the previous note:

If you want to emulate this function in PHP4, you need to return the bytes written as well as support for arrays, flags.

I can only figure out the FILE_APPEND flag and array support. If I could figure out "resource context" and the other flags, I would include those too.

<?

define
('FILE_APPEND', 1);
function
file_put_contents($n, $d, $flag = false) {
  
$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
  
$f = @fopen($n, $mode);
   if (
$f === false) {
       return
0;
   } else {
       if (
is_array($d)) $d = implode($d);
      
$bytes_written = fwrite($f, $d);
      
fclose($f);
       return
$bytes_written;
   }
}

?>
17-Jul-2006 04:24
simple function for php4:

function file_put_contents($n,$d) {
  $f=@fopen($n,"w");
  if (!$f) {
   return false;
  } else {
   fwrite($f,$d);
   fclose($f);
   return true;
  }
}
sendoshin at awswan dot com
06-Mar-2006 03:01
To clear up what was said by pvenegas+php at gmail dot com on 11-Oct-2005 08:13, file_put_contents() will replace the file by default.  Here's the complete set of rules this function follows when accessing a file:

1.  Was FILE_USE_INCUDE_PATH passed in the call?  If so, check the include path for an existing copy of *filename*.

2.  Does the file already exist?  If not, first create it in the current working directory.  Either way, open the file.

3.  Was LOCK_EX passed in the call?  If so, lock the file.

4.  Was the function called with FILE_APPEND?  If not, clear the file's contents.  Otherwise, move to the end of the file.

5.  Write *data* into the file.

6.  Close the file and release any locks.

If you don't want to completely replace the contents of the file you're writing to, be sure to use FILE_APPEND (same as fopen() with 'a') in the *flags*.  If you don't, whatever used to be there will be gone (fopen() with 'w').

Hope that helps someone (and that it makes sense ^^)!

- Sendoshin
pvenegas+php at gmail dot com
12-Oct-2005 10:13
Note that if the specified file already exists, this function effectively discards its contents (equivalent to fopen with 'w') and inserts the new data. The documentation doesn't say this explicitly, so this might help those who are unsure.
Jared Kuolt
05-Mar-2005 04:14
Note that this function will create the file if it does not exists, assuming PHP has write access to the folder.
aidan at php dot net
21-May-2004 10:11
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat