Phpfile_put_contents function _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Phpfile_put_contents function. Phpfile_put_contents function file_put_contents (PHP5) file_put_contents-write a string to a file description international file_put_contents (string $ file name, mixed $ Data php file_put_contents function

File_put_contents
(In PHP 5)

File_put_contents-write a string to a file

Description
International file_put_contents (string $ file name, mixed $ Data [Summary $ flag = 0 [, resource $ background])
This function requires the fopen () function, fwrite () and fclose () to write data into a file successively.

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

Parameters

File name
Where the file path is written.

Data
This data is written to us. It can be a string, array, or stream resource (as described above ).

If the data flow resource is used, the stream in the remaining buffer zone 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 is equivalent to file_put_contents ($ file name, burst ('', $ Array )).

Flag
The value of the flag can be any combination of the following flags (with some limitations), the binary or (|) operator added.


Description of flag hanging
FILE_USE_INCLUDE_PATH: The Search file name contains a directory. For more information, see include_path.
FILE_APPEND if the file filename already exists, append the data file instead of overwriting it.
LOCK_EX gets an exclusive locked file and starts writing at the same time.
The text mode in which FILE_TEXT 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 encoding. This flag cannot be used for FILE_BINARY. This flag only applies to PHP 6.
FILE_BINARY data will be written to binary mode. This is the default setting and cannot be used for FILE_TEXT. This flag only applies to PHP 6.


Background
Create stream_context_create () within the effective range of the resource ().


Return value
The number of bytes returned by this function is a failure to write the file or FALSE.

Instance

Example #1 Simple example

$ File = 'lele.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 = 'lele.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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445472.htmlTechArticlephp 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...

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.