Learning PHP
Advanced PHP
PHP Function Reference
PHP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM
|
PHP Function file_put_contents()
Syntax
int file_put_contents ( string $filename, mixed $data [, int $flags [, resource $context]] );
|
Definition and Usage
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flags is set.
Paramters
Parameter | Description |
filename | Path to the file where to write the data. |
data | The data to write. Can be either a string, an array or a stream resource |
flags | The value of flags can be any combination of the following flags (with some restrictions), joined with the binary OR (|) operator.
- FILE_USE_INCLUDE_PATH: Search for filename in the include directory.
- FILE_APPEND: If file filename already exists, append the data to the file instead of overwriting it.
- LOCK_EX: Acquire an exclusive lock on the file while proceeding to the writing.
- FILE_TEXT: data is written in text mode. This flag cannot be used with FILE_BINARY. This flag is only available since PHP 6.
- FILE_BINARY: data will be written in binary mode. This is the default setting and cannot be used with FILE_TEXT. This flag is only available since PHP 6.
|
context | A valid context resource created with stream_context_create(). |
Return Value
The function returns the amount of bytes that were written to the file, or FALSE on failure.
Example
Following is the usage of this function:
<?php
// Copy input file into output file.
$inputFile = "/home/httpd/inputFile.txt";
$outputFile = "/home/httpd/outputFile.txt";
$fi = fopen($inputFile, 'r');
$source = '';
while (!feof($fi)) {
$source .= fgets($fi);
}
fclose($fi);
file_put_contents($outputFile,$source);
?>
|
|
|
|