Php file processing-writing files and operating files

Source: Internet
Author: User
In PHP file processing, you can open and close the file and file reading, and read a row, character, string, and the entire file. since you have read the file, you must have written it, in this article, we will explain how to write and operate files! Php file processing-writing files and operating files

In PHP file processing, you can open and close the file and file reading, and read a row, character, string, and the entire file. since you have read the file, you must have written it, in this article, we will explain how to write and operate files!

We just mentioned the first three articles, php file processing-opening/closing a file, and PHP file processing-reading a file (one character, string) "and" php file processing-how to read a file (one row, the entire file) "are described in detail. if you have not read any of them, you can check them out! Today, we will mainly explain how to write files and files.

I. writing data to a file

Writing data is also a common file operation in PHP. in PHP, use the fweite () and file_put_contents () functions to write data to the file.

1. when fwrite () can be called fputs (), the usage of both of them is the same. The syntax of the fwrite () function is as follows:

int fwrite ( resource $handle , string $string [, int $length ] )

This function writes the content string to the file pointed to by the pointer handle. If the length parameter is specified, write the length byte and stop it. if the file content length is smaller than the length, all the file content will be output.

2. the file_put_contents () function is a new function in PHP5. the syntax format of this function is as follows:

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
  • Filename is the file to write data.

  • Data is the data to be written.

  • Flags can be FILE_USE_INCLUDE_PATH, FILE_APPEND or LOCK_EX. LOCK_EX is an exclusive lock. we will introduce it in detail in the following article!

Using the file_put_contents () function is the same as calling the fopen (), fwrite (), and fclose () functions in turn. Next we will compare the advantages of functions through specific strength!

In this example, the fwrite () function is used to write data to the file, and the data is written using the file_put_contents () function. the sample code is as follows:

 Use the file_put_contents function to write files: "; file_put_contents ($ filepath, $ str); readfile ($ filepath);?>

Output result:

}

Note:

When reading and writing files, except for a few functions such as file () and readfile (), you must use the fopen () function to open the file before using fclose () function to close the file. file information functions (such as filesize and filemtime) do not need to open the file, as long as the file is saved!

PHP file processing ends here. the following article processes the PHP Directory. For more information, see PHP directory processing-open/close directory.

The above is the PHP file processing-detailed content for writing files and operating files. For more information, see other related articles in the first PHP community!

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.