In PHP website development, there are usually two ways to store data. One is stored in text files, such as txt files, and the other is stored in databases, such as Mysql. Compared with database storage, file storage has no advantages, but File reading and writing operations are still used from time to time in basic PHP development. Today I will share with you a file writing operation tutorial on how to use PHP technology to realize file reading and writing. It can also be regarded as an introduction to PHP file reading and writing operations.
The operation of writing data to a file mainly involves three steps and some file operation functions are 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 will explain through the file reading and writing operation code example tutorial
Basic PHP file writing operation functions fopen, fwrite, fclose application Tutorial
Copy code The 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 website development tutorial network, a PHP tutorial network for PHP beginners. "."n";
$fileData = $fileData."title"."t"."This article mainly talks about the most basic file writing tutorial in PHP file reading and writing operations. ";
fwrite($fp. ,$fileData);
fclose($fp);
}
?>
Note: In this file read and write operation example code, the main function is The file writes two lines of text.
Knowledge points:
1. Use the fopen function to open a file. When applying the fopen function to prepare to open a file, you first need to clarify:
What to open the file for? Is it reading the data in the file, writing the data to the file, or reading and writing the file?
In addition, you need to consider if relevant data already exists in the file, should you overwrite the data in the original file, or just add new data to the end of the file
These issues involve fopen in PHP file read and write operations For the application of file mode in the function, the fopen function prototype is as follows:
Copy code The code is as follows:
fopen(filename,mode ,include_path,context)
When calling the file operation function fopen(), you usually need to pass two or three parameters.
Filename: Specifies the file or URL to be opened. You can specify the absolute path of the file, usually C: for Windows and / for Unix. You can also open remote files through URLs. The file written here is in the same directory as the PHP file writing code file I placed.
Mode: Specifies the type of access required to the file/stream. That is, the mode in which the file is opened.
include_path: optional. 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” - Open the file in read-only mode and start reading from the file header.
"r+" - Open the file for reading and writing.
"w" - Open the file for writing and start writing from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
"w+" - Open the file for reading and writing, starting from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
"a" - Open for writing, append writing starting from the end of the file. If the file does not exist then attempts to create it.
"a+" - Open in read-write mode, append writing or reading starting from the end of the file. If the file does not exist then attempts to create it.
Note: When performing file read and write operations, you must ensure that the file you open has the corresponding read and write permissions, otherwise fopen will report an error. You can use @ to suppress errors and handle them appropriately.
2. After using the file operation function fopen to open the file, you need to assign a value to the variable and then write it to the file pointer pointed to by $fp. In the above example of the PHP file writing operation tutorial, I used line by line Storage, that is, newline storage, mainly uses n as the newline separator.
The fwrite file writing function prototype is as follows:
Copy code The code is as follows:
fwrite(fp,string,length )
Here you can also use the file writing function fputs, which is an alias function of fwrite. Its function and usage are the same as fwrite.
In the file writing function fwrite, length is an optional option. It is mainly used to set the maximum number of characters to write to the file. If this parameter is set, fwrite will write the specified file according to the set length. Write the specified length of characters. fwrite() returns the number of characters written to the file, or false if an error occurs.
After the file writing operation is completed, the file handle needs to be closed, otherwise system resources will be occupied. You can use the fclose($fp) function to accomplish this. Returns true if the file is closed successfully, otherwise returns false.
At this point, the file writing operation is completed.
The above is the most basic application of file writing operation in PHP file reading and writing operation tutorial. In addition to file writing operation, in PHP website development, it is often necessary to read related file contents. In the file reading and writing operation function You can use different functions to read files. Next time I will explain how to read the file.
http://www.bkjia.com/PHPjc/322837.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322837.htmlTechArticleIn PHP website development, there are usually two ways to store data, one is stored in text files, such as txt Files are stored in a database, such as Mysql, relative to data inventory...