search for in the  
<file_get_contentsfile>
Last updated: Thu, 19 May 2005

file_put_contents

(PHP 5)

file_put_contents -- Write a string to a file

Description

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

Identical to calling fopen(), fwrite(), and fclose() successively. The function returns the amount of bytes that were written to the file.

flags can take FILE_USE_INCLUDE_PATH and/or FILE_APPEND, however the FILE_USE_INCLUDE_PATH option should be used with caution.

You can also specify the data parameter as an array (not multi-dimension arrays). This is equivalent to file_put_contents($filename, join('', $array)).

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CXXII, Stream Functions.

As of PHP 5.1.0, you may also pass a stream resource to the data parameter. In result, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream().

Note: This function is binary-safe.

Tip: You can use a URL as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename and Appendix L for a list of supported URL protocols.

See also fopen(), fwrite(), fclose(), and file_get_contents().



User Contributed Notes
file_put_contents
Séb.
26-Apr-2005 01:05
About comment @08-Mar-2005 02:22, the proposed function is not compatible with PHP5 file_put_contents( ) :
- Nothing about array
- Nor about flags and constants
- Fatal error instead of warning
- fopen( ) mods 'a+' or 'w+' are unuseful for a writting function
- Returns nothing *instead of* FALSE or length written
=> Avoid this function.

If you don't want to install PEAR, just download the PHP_Compat package and copy/past the function file_put_contents( ) given.
http://pear.php.net/package/PHP_Compat
08-Mar-2005 07:22
This useful tip seems to have disappeared from here recently...

To use file_put_contents prior to PHP5 and without having to upgrade/install PEAR, use this:

if(!function_exists('file_put_contents')) {
  function file_put_contents($filename, $data, $file_append = false) {
   $fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
   if(!$fp) {
     trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
     return;
   }
   fputs($fp, $data);
   fclose($fp);
  }
}
Jared Kuolt
04-Mar-2005 02: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
20-May-2004 09: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

<file_get_contentsfile>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT