since php 5.2.1 $data can also be an object that implements the __toString() method..
<?php
class foo {
public function __toString()
{
return "write this";
}
}
file_put_contents(sys_get_temp_dir() . '/test.txt', new foo(), LOCK_EX);
?>
/tmp/test.txt will contain "write this" now. ;-)
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.
You can also specify the data parameter as an array (not multi-dimension arrays). This is equivalent to file_put_contents($filename, implode('', $array)).
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().
Parameters
- filename
The file name where to write the data
- data
The data to write. Can be either a string, an array or a stream resource (explained above).
- flags
flags can take FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX (acquire an exclusive lock), however the FILE_USE_INCLUDE_PATH option should be used with caution.
- context
A context resource
Return Values
The function returns the amount of bytes that were written to the file, or FALSE on failure.
ChangeLog
| Version | Description |
|---|---|
| 5.1.0 | Added support for LOCK_EX and the ability to pass a stream resource to the data parameter |
Notes
Note: This function is binary-safe.
Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams.
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 O, List of Supported Protocols/Wrappers for a list of supported URL protocols.
See Also
| fopen() |
| fwrite() |
| file_get_contents() |
| stream_context_create() |
file_put_contents
30-Apr-2007 08:10
15-Feb-2007 06:41
If you want to append data to a file using a lock use the following
file_put_contents ('sample.txt', 'some data', FILE_APPEND | LOCK_EX);
21-Dec-2006 11:20
As to the previous user note, it would be wise to include that code within a conditional statement, as to prevent re-defining file_put_contents and the FILE_APPEND constant in PHP 5:
<?php
if ( !function_exists('file_put_contents') && !defined('FILE_APPEND') ) {
...
}
?>
Also, if the file could not be accessed for writing, the function should return boolean false, not 0. An error is different from 0 bytes written, in this case.
23-Jul-2006 11: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;
}
}
?>
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;
}
}
06-Mar-2006 07: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
12-Oct-2005 02: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.
04-Mar-2005 08:14
Note that this function will create the file if it does not exists, assuming PHP has write access to the folder.
21-May-2004 02: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
