PHP file read and write operation of file write code _php tips

Source: Internet
Author: User
Tags function prototype php tutorial php website
In the development of PHP website, there are usually two ways to store data, a text file storage, such as TXT file, a database storage, such as MySQL, compared to database storage, file storage does not have any advantages, but the file read and write operations in the basic PHP development or when there is use, Today and you share how to use the PHP technology to achieve file read and write file write operations tutorial, is also a PHP file read and write operation of the introductory learning.
The operation of writing data to a file involves three steps and some file operation functions as follows:
1, open the file (file operation function: fopen)
2, write the file (file operation function: Fwrite, etc.)
3, close the file (file operation function: fclose)
The following is a tutorial on file read and write operation code examples
Basic PHP File write operation function fopen, fwrite, fclose application Tutorials
Copy Code code as follows:

?
@ $fp = fopen ("Leapsoulcn.txt", "w");
if (! $fp) {
echo "System error";
Exit ();
}else {
$fileData = "domain". " \ t "." Www.leapsoul.cn "." \ n ";
$fileData = $fileData. " Description "." \ t "." PHP Web site Development Tutorial Network, PHP Beginners for the PHP tutorial network. "." \ n ";
$fileData = $fileData. " Title "." \ t "." This article mainly describes the PHP file read and write operation of the most basic file writing tutorial. ";
Fwrite ($fp, $fileData);
Fclose ($FP);
}
?>

Note: In this section of the file read-write operation instance code, the main function is to write two lines of text in the file.
Knowledge Points:
1, using the fopen function to open the file, in the application of the fopen function to open the file, you first need to be clear:
Why do you open the file? Is it reading the data in the file, or writing the data to the file, or reading or writing the file?
In addition, you need to consider whether you overwrite the data in the original file if the relevant data already exists in the file, or simply add the new data to the end of the file.
These issues relate to the application of file patterns in fopen functions in PHP file read-write operations, fopen function prototypes are as follows:
Copy Code code as follows:

fopen (Filename,mode,include_path,context)

when calling a file action function fopen (), you typically need to pass two or three arguments.
FileName: Specify the file or URL to open. You can specify the absolute path to the file, Windows is generally C:\,unix/, and you can open the remote file by using the URL. The files written here are written to the PHP file with the code file I put in the same directory.
Mode: Specify the type of access required to the file/stream. The mode in which the file is opened.
Include_path: Options available. If you need to search for files in include_path, you can set this argument to 1 or true.
Common fopen file operation mode description
"R"-opens the file as read-only and reads from the file header.
"R+"-opens the file in read-write mode.
"W"-Opens the file as write, starting at the file header. If the file does not exist, try to create, and if the file exists, delete the contents of the file first.
"w+"-Read and write to open the file, starting from the file header. If the file does not exist, try to create, and if the file exists, delete the contents of the file first.
' A '-opens as write, append write from end of file. Try to create if the file does not exist.
"A +"-read-write to open, append write or read from the end of the file. Try to create if the file does not exist.
Note: In the file read and write operation, you must ensure that the open file has the appropriate read and write permissions, or fopen will be an error. You can use @ to suppress the resulting error, and then properly handle the error.
2, after using the file Operation function fopen Open the file, you need to assign a value to a variable, and then write to the file pointer that $fp points to, and in the example of the php file write operation tutorial, I used a row of storage, the newline store, mainly by \ n as a newline separator.
The Fwrite file Write function prototype is as follows:
Copy Code code as follows:

Fwrite (Fp,string,length)

Here you can also use the file Write function fputs, which is the fwrite alias function, with the same function as fwrite.

Length is optional in the file write function fwrite, which is used to set the maximum number of characters to write to the file, and if this parameter is set, Fwrite will write the specified length of characters in the designated file according to the set lengths. Fwrite () returns the number of characters written to the file, False when an error occurs.

After the file write operation is complete, the file handle needs to be closed, otherwise the system resources will be consumed. You can use the fclose ($fp) function to do this. Returns true if the file is closed successfully, or false.

The file write operation is complete.

  The above is the PHP file read and write operation tutorial file write operation of the most basic application, in addition to file write operation, in PHP website development often need to read the relevant file content, file read and write operation function can use different functions to achieve the read operation of the file. The next time you will be reading the file.

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.