PHP file_put_contents writes or appends a string to a file

Source: Internet
Author: User

In php, The file_put_contents function of the file can write our strings to the file. This is the same as the php fwrite file. Let's take a look at the difference between file_put_contents usage and fwrite.

The PHP file_put_contents () function is the most suitable option for writing strings or appending string content to a file at a time.

File_put_contents ()

The file_put_contents () function is used to write a string to a file. The number of bytes written to the file is returned. If the string fails, FALSE is returned.

Example:

The Code is as follows: Copy code

<? Php
Echo file_put_contents ("test.txt", "This is something .");
?>

Run this example and the browser outputs:

18
The content of the test.txt file (in the same directory as the program) is: This is something ..

Prompt

• If the file does not exist, the file is created, which is equivalent to the fopen () function.
• If the file exists, content in the file will be cleared by default. You can set the flags parameter value to FILE_APPEND to avoid this (see below ).
• This function can be safely used for binary objects.
Write content as an append
When the flags parameter value is set to FILE_APPEND, it indicates that new data is written by appending content after the existing file content:

The Code is as follows: Copy code

<? Php
File_put_contents ("test.txt", "This is another something.", FILE_APPEND );
?>

After the program is executed, the content of the test.txt file becomes: This is something. This is another something.

File_put_contents () actually calls the fopen (), fwrite (), and fclose () functions in turn.

So what is the difference between file_put_contents and fwrite?

The following is the code of the file_put_contents instance:

The Code is as follows: Copy code

<? Php
$ Filename = 'file.txt ';
$ Word = "Hello! Rnwebkaka "; // double quotation marks wrap single quotation marks without line breaks
File_put_contents ($ filename, $ word );
?>

The same function uses fwrite instance code:

The Code is as follows: Copy code

<? Php
$ Filename = 'file.txt ';
$ Word = "Hello! Rnwebkaka "; // double quotation marks wrap single quotation marks without line breaks
$ Fh = fopen ($ filename, "w"); // w writes the appended write from the beginning
Echo fwrite ($ fh, $ word );
Fclose ($ fh );
?>

From the above two examples, we can see that file_put_contents is a simplified method of fopen, fwrite, and fclose, which is good for program code optimization. On the one hand, it reduces the amount of code, on the other hand, there will be no less rigorous code written by fclose, which is much more convenient for debugging and maintenance.

In the above example, file_put_contents is written from the beginning. What should I do if I want to append the data?

In the syntax of file_put_contents, there is a parameter FILE_APPEND, which is the declaration of append writing. The instance code is as follows:

The Code is as follows: Copy code

<? Php
Echo file_put_contents('file.txt ', "This is another something.", FILE_APPEND );
?>

FILE_APPEND is the declaration of append writing. During append writing, in order to prevent other users from simultaneously operating, you often need to lock the file. In this case, you need to add another LOCK_EX statement as follows:

The Code is as follows: Copy code

<? Php
Echo file_put_contents('file.txt ', "This is another something.", FILE_APPEND | LOCK_EX );
?>

Note: The echo output in the above Code is the length of the file string written to the display.

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.