Php file write operations

Source: Internet
Author: User
Tags function prototype php tutorial

Php Tutorial file write operations
Writing data to a file involves three steps and some file operation functions:
1. Open the file (File operation function: fopen)
2. Write files (File operation functions: fwrite, etc)
3. Close the file (File operation function: fclose)
The following is an example of the file read/write operation code.
Basic PHP file write operation functions fopen, fwrite, fclose application tutorial
1. Use the fopen function to open a file. When applying the fopen function to open a file, you must first make it clear:
What do I do when I open a file? Do you want to read or write data into a file?
In addition, you need to consider whether to overwrite the data in the original file if the file already contains the relevant data, or simply add the new data to the end of the file.
These problems involve the application of the file mode in the fopen function in php file read/write operations. The fopen function prototype is as follows:
Copy the code as follows:
Fopen (filename, mode, include_path, context)

When calling the file operation function fopen (), two or three parameters are usually required.
Filename: specifies the file or url to be opened. You can specify the absolute path of the file. windows generally uses c:, unix as/. You can also open the remote file through the url. The file written here is in the same directory as the code file written to the PHP file.
Mode: specifies the access type of the file/stream. That is, the file opening mode.
Include_path: Optional. If you need to search for a file in include_path, you can set this parameter to 1 or true.
Description of common fopen file operation modes
"R"-open the file in read-only mode and read it from the file header.
"R +"-open a file in read/write mode.
"W"-open the file in writing mode and start writing from the file header. If the object does not exist, try to create it. If the object exists, delete the object content first.
"W +"-open the file in read/write mode and read and write from the file header. If the object does not exist, try to create it. If the object exists, delete the object content first.
"A"-open as a write, and append the write from the end of the file. If the file does not exist, try to create it.
"A +"-open in read/write mode and append write or read from the end of the file. If the file does not exist, try to create it.
Note: When performing file read/write operations, make sure that the opened file has the corresponding read/write permissions; otherwise, an error is returned for fopen. You can use @ to suppress the generated errors and handle them properly.
2. After opening a file using the file operation function fopen, you need to assign a value to the variable and write it to the file pointer pointed to by $ fp. In the above php file write operation tutorial instance, I use a row and a row storage, that is, a line feed storage, using n as the line feed separator.
The fwrite file writing function prototype is as follows:
Copy the code as follows:
Fwrite (fp, string, length)

You can also use the file writing function fputs, which is the alias function of fwrite. The Function and usage are the same as that of fwrite.

In the file writing function fwrite, length is optional. It is mainly used to set the maximum number of characters written to the file. If this parameter is set, fwrite will follow the set length, write the specified length of characters in the specified file. Fwrite () returns the number of characters written to the file. If an error occurs, false is returned.

After the file write operation is complete, you need to close the file handle, otherwise it will occupy system resources. You can use the fclose ($ fp) function to complete this task. If the object is successfully closed, true is returned; otherwise, false is returned.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.