Home > Backend Development > PHP Tutorial > php file_put_contents function_PHP tutorial

php file_put_contents function_PHP tutorial

WBOY
Release: 2016-07-20 11:00:58
Original
1439 people have browsed it

php file_put_contents function ​

file_put_contents
(in PHP 5)

file_put_contents - writes a string to a file

Description
int file_put_contents (string $filename, mixed$data[summary$flag=0[, resources$background]])
This function is identical to the requirements of fopen(), fwrite(), and fclose() which successively write data to a file.

If the file does not exist, the file is created. Otherwise, the existing file is overwritten unless the FILE_APPEND flag is set.

Parameters

File name
File path where to write the data.

Data
Write to us about this data. Can be a string, array or stream resource (explained above).

If the data stream is a resource, the remaining buffer of the stream will be copied to the specified file. This is similar to using stream_copy_to_stream().

You can also specify the data parameter as a single level array. This is equivalent to file_put_contents($filename, explode('', $array)).

Flag
Flag values ​​can be any combination of the following flags (with some restrictions), added by the binary or ( | ) operator.


Description of flags that can be flown
FILE_USE_INCLUDE_PATH searches for directories contained in file names. See include_path for more information.
FILE_APPEND If the archive filename already exists, append data to the file instead of overwriting it.
LOCK_EX obtains an exclusive lock on the file and begins writing.
FILE_TEXT text mode for data writing. If Unicode semantics are enabled, the default character encoding is UTF-8. You can specify a different encoding, establish a custom range or use stream_default_encoding() to change the default. This flag cannot be used with FILE_BINARY. This flag only works 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 only works since PHP 6.


Background
Establish stream_context_create() within the valid scope of the resource.


Return value
This function returns the number of bytes written to the file, or FALSE on failure.

Example

Example #1 using simple example

$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
//Append a new person to the file
$current .= "John Smithn";
// Write the contents back to the file
file_put_contents($file, $current);
?>


$file = 'people.txt';
// The new person to add to the file
$person = "John Smithn";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445472.htmlTechArticlephp file_put_contents function file_put_contents (PHP 5) file_put_contents - writes a string to a file description international file_put_contents (String $filename, mixed $data...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template