PHP file read and write operation file write code _php tutorial

Source: Internet
Author: User
In the development of PHP Web site, there are usually two ways to store data, a text file storage, such as TXT file, one is stored in a database, such as MySQL, relative to the database storage, file storage has no advantage, but the file read and write operations in the basic PHP development is still used, Today to share with you how to use PHP technology to achieve file read and write file writing tutorial, but also is the PHP file read and write operation of the introductory learning.
The operation of writing data to a file mainly involves three steps and some file operation functions as follows:
1. Open file (file operation function: fopen)
2, write file (file operation function: Fwrite, etc.)
3. Close file (file operation function: fclose)
The following is an example of a file read and write operation code Tutorial to explain
Basic PHP File write operation function fopen, fwrite, fclose application tutorial
Copy CodeThe code is 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 Development Tutorial Network, for PHP beginners PHP Tutorial Network. "." \ n ";
$fileData = $fileData. " Title "." \ t "." This article mainly describes the PHP file read and write operations in the most basic file writing tutorial. ";
Fwrite ($fp, $fileData);
Fclose ($FP);
}
?>

Note: In this file read and write operation instance code, the main function is to write two lines of text in the file.
Knowledge Points:
1, use the fopen function to open the file, when the application fopen function ready to open the file, you need to clear:
What do you open the file for? Read the data in a file, or write it to a file, or read or write a file?
Also you need to consider if the data already exists in the file, do you overwrite the data in the original file, or just add the new data to the end of the file?
These issues relate to the application of file patterns in the fopen function in PHP file read and write operations, and the Fopen function prototype is as follows:
Copy CodeThe code is as follows:
fopen (Filename,mode,include_path,context)

when invoking the file manipulation function fopen (), it is usually necessary to pass two or three arguments.
FileName: Specifies the file or URL to open. You can specify the absolute path to the file, Windows is generally C:\,unix/, and you can open remote files by URL. Here write the file with PHP file write code file I put in the same directory.
Mode: Specifies 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 parameter to 1 or true.
Description of commonly used fopen file operation modes
"R"-opens the file as read-only and starts reading from the file header.
"R+"-opens the file as read-write.
"W"-Open the file as written and start writing 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.
"W+"-open the file as read-write, read and write 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"-open in writing, append write from the end of the file. If the file does not exist, try to create it.
"A +"-opens in read-write mode and appends write or read from the end of the file. If the file does not exist, try to create it.
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 error. You can use @ to suppress the resulting error, and then handle the error appropriately.
2, after the use of the file Operation function fopen Open the file, you need to assign a value to the variable, and then write to $fp point to the file pointer, in the above PHP file write operation tutorial instance, I use a row of storage, that is, newline storage, mainly through \ n as a line break delimiter.
The Fwrite file Write function prototype is as follows:
Copy CodeThe code is as follows:
Fwrite (Fp,string,length)

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

In the file write function fwrite length is optional, mainly used to set the maximum number of characters written to the file, if this parameter is set, the fwrite will be set length, in the specified file to write a defined amount of characters. Fwrite () returns the number of characters written to the file, or False when an error occurs.

After the file write operation is complete, you need to close the file handle, otherwise it will consume system resources and so on. You can use the fclose ($fp) function to do this. Returns true if the file is closed successfully, otherwise false.

The file write operation is complete.

The above is the PHP file read and write operation of the most basic application of the file write operation, in addition to file write operations, in the development of PHP Web site often need to read the relevant file content, file read and write operation function can use different functions to implement the read operation of the file. Next time you will be divided how to read the file.

http://www.bkjia.com/PHPjc/322837.html www.bkjia.com true http://www.bkjia.com/PHPjc/322837.html techarticle in the development of PHP Web site, there are usually two ways to store data, a text file storage, such as TXT file, one is stored in a database, such as MySQL, relative to the data inventory ...

  • 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.