PHP file_put_contents function _php Tutorial

Source: Internet
Author: User
PHP file_put_contents functions

File_put_contents
(in PHP 5)

File_put_contents-Write a string to a file

Describe
International file_put_contents (string $ file name, mixed $ data [abstract $ flag = 0 [, resource $ background]])
This feature is identical to the requirement fopen () function, fwrite () and fclose () to write data to a file successively.

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

Parameters

Filename
The file path where the data is written.

Data
This data is a letter to us. Can be a string, an array, or a stream resource (explained above).

If the data flow 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 data parameters as a single layer array. This equates to file_put_contents ($ file name, Burst (', $ array)).

Flag
The value of the national flag can be arbitrarily combined with the following flags (with some restrictions), added binary or (| ) operator.


Flag can be hoisted description
File_use_include_path The search file name contains a directory. See Include_path for more information.
File_append if the archive filename already exists, append the data to the file instead of overwriting it.
LOCK_EX obtains an exclusive locked file and begins writing at the same time.
File_text the text mode in which the data is written. If Unicode semantics is enabled, the default character encoding is UTF-8. You can specify a different encoding, create a custom range, or use stream_default_encoding () to change the default. This flag cannot be used for file_binary. This flag is only available from PHP 6.
The file_binary data is written in binary mode. This is the default setting and cannot be used for file_text. This flag is only available from PHP 6.


Background
Establish Stream_context_create () within the valid range of resources.


return value
The number of bytes returned by the function is a failure to write to the file, or false.

Instance

For example # 1 use a 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);
?>


http://www.bkjia.com/PHPjc/445472.html www.bkjia.com true http://www.bkjia.com/PHPjc/445472.html techarticle php file_put_contents function file_put_contents (PHP 5) file_put_contents-Write a string to a file Description international file_put_contents (string $ file name, mixed $ data ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.