In PHP website development, there are usually two ways to store data. One is to store it in a text file, such as txt file, and the other is to store it in a database, such as Mysql. Compared with database storage, file storage does not have any advantages, but file reading and writing operations are still used frequently in basic PHP development. Today I will share with you the tutorial on how to use PHP technology to realize file reading and writing operations, which can be regarded as an introductory learning of 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 to the file (file operation function: fwrite, etc.)
3. Close the file (file operation function: fclose)
The following is an explanation through the example tutorial on file reading and writing operation code
Basic PHP file write operation functions fopen, fwrite, and fclose application tutorial
<?
@$fp = fopen("","w");
if(!$fp){
echo "system error";
exit();
}else {
$fileData = "domain"."\t".""."\n";
$fileData = $fileData."description"."\t"."PHP website development tutorial network, PHP tutorial network for beginners of PHP. "."\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 to write two lines of text in the file.
Knowledge points:
1. Use the fopen function to open the file. When applying the fopen function to prepare to open the file, you first need to clarify:
What do you do when opening a file? Should I read the data in a file, write the data to a file, or read and write 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 the new data to the end of the file
These problems involve the application of file mode in fopen function in PHP file read and write operations. The fopen function prototype is as follows:
fopen(filename,mode,include_path,context)
When calling the file operation function fopen(), two or three parameters are usually required to be passed.
filename: specifies the file or URL to be opened. You can specify the absolute path of the file. Windows is generally C:\, Unix is /, and through URL, you can also open remote files. The file written here is written to the code file as the PHP file I put in the same directory.
mode: specifies the access type required to the file/stream. That is, the mode of opening the file.
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 mode
"r" - Open the file in read-only mode and start reading from the file header.
"r+" - Open the file in read and write mode.
"w" - Open the file in writing and start writing from the file header. If the file does not exist, try to create it, and if the file exists, delete the contents in the file first.
"w+" - Open the file in a read-write manner, starting from the file header. If the file does not exist, try to create it, and if the file exists, delete the contents in the file first.
"a" - Open as write, append writes from the end of the file. If the file does not exist, try to create it.
"a+" - Open in read and write mode, and add write or read from the end of the file. If the file does not exist, try to create it.
Note: When performing file read and write operations, you must ensure that the opened file has corresponding read and write permissions, otherwise fopen will report an error. You can use @ to suppress the errors generated and then handle the errors reasonably.
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 use line-by-line storage, that is, line-break storage, mainly through \n as line-break separator.
The prototype of the fwrite file writing function is as follows:
fwrite(fp,string,length)
Here you can also use the file to write function fputs, which is an alias for fwrite, and its functions and usage are the same as fwrite.
In the file writing function fwrite, length is an option, which is mainly used to set the maximum number of characters to write to the file. If this parameter is set, fwrite will write characters of the specified length in the specified file according to the set length. fwrite() returns the number of characters written to the file, and returns false when an error occurs.
After the file writing operation is completed, the file handle needs to be closed, otherwise it will occupy system resources, etc. This can be done using the fclose($fp) function. Return true if the file is closed successfully, otherwise return false.
At this point, the file writing operation is completed.
The above is the most basic application of file writing operations in the PHP file read and write operation tutorial. In addition to file writing operations, related file content is often required in PHP website development. Different functions can be used to implement file reading operations. Next time, we will divide the file to read.
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 to the file (file operation function: fwrite, etc.)
3. Close the file (file operation function: fclose)
The following is an explanation through the example tutorial on file reading and writing operation code
Basic PHP file write operation functions fopen, fwrite, and fclose application tutorial
Copy the codeThe code is as follows:
<?
@$fp = fopen("","w");
if(!$fp){
echo "system error";
exit();
}else {
$fileData = "domain"."\t".""."\n";
$fileData = $fileData."description"."\t"."PHP website development tutorial network, PHP tutorial network for beginners of PHP. "."\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 to write two lines of text in the file.
Knowledge points:
1. Use the fopen function to open the file. When applying the fopen function to prepare to open the file, you first need to clarify:
What do you do when opening a file? Should I read the data in a file, write the data to a file, or read and write 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 the new data to the end of the file
These problems involve the application of file mode in fopen function in PHP file read and write operations. The fopen function prototype is as follows:
Copy the codeThe code is as follows:
fopen(filename,mode,include_path,context)
When calling the file operation function fopen(), two or three parameters are usually required to be passed.
filename: specifies the file or URL to be opened. You can specify the absolute path of the file. Windows is generally C:\, Unix is /, and through URL, you can also open remote files. The file written here is written to the code file as the PHP file I put in the same directory.
mode: specifies the access type required to the file/stream. That is, the mode of opening the file.
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 mode
"r" - Open the file in read-only mode and start reading from the file header.
"r+" - Open the file in read and write mode.
"w" - Open the file in writing and start writing from the file header. If the file does not exist, try to create it, and if the file exists, delete the contents in the file first.
"w+" - Open the file in a read-write manner, starting from the file header. If the file does not exist, try to create it, and if the file exists, delete the contents in the file first.
"a" - Open as write, append writes from the end of the file. If the file does not exist, try to create it.
"a+" - Open in read and write mode, and add write or read from the end of the file. If the file does not exist, try to create it.
Note: When performing file read and write operations, you must ensure that the opened file has corresponding read and write permissions, otherwise fopen will report an error. You can use @ to suppress the errors generated and then handle the errors reasonably.
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 use line-by-line storage, that is, line-break storage, mainly through \n as line-break separator.
The prototype of the fwrite file writing function is as follows:
Copy the codeThe code is as follows:
fwrite(fp,string,length)
Here you can also use the file to write function fputs, which is an alias for fwrite, and its functions and usage are the same as fwrite.
In the file writing function fwrite, length is an option, which is mainly used to set the maximum number of characters to write to the file. If this parameter is set, fwrite will write characters of the specified length in the specified file according to the set length. fwrite() returns the number of characters written to the file, and returns false when an error occurs.
After the file writing operation is completed, the file handle needs to be closed, otherwise it will occupy system resources, etc. This can be done using the fclose($fp) function. Return true if the file is closed successfully, otherwise return false.
At this point, the file writing operation is completed.
The above is the most basic application of file writing operations in the PHP file read and write operation tutorial. In addition to file writing operations, related file content is often required in PHP website development. Different functions can be used to implement file reading operations. Next time, we will divide the file to read.