File write code for PHP file read and write operations

  • 2020-03-31 21:26:18
  • OfStack

In PHP web development, there are usually two ways to store data, a way of to a text file storage, TXT file, for example, a way is stored in database, Mysql, for example, relative to the database storage, file storage and there is no advantage, but the file read and write operations is often used in the basic PHP development, share with you today how to use the PHP technology implementation file to read and write files to write tutorial, is to start learning PHP file to read and write operations.
The operation of writing data to a file mainly involves three steps and some file operation functions as follows:
1. Open the file (file operation function: fopen)
2. Write to file (file operation function: fwrite, etc.)
3. Close the file (file operation function: fclose)
The following is an example of a file read-write code tutorial
Basic PHP file writing operation functions fopen, fwrite, fclose application tutorial
 
<? 
@$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 net, oriented PHP A beginner's PHP Tutorial on web. "."n"; 
$fileData = $fileData."title"."t"." This paper mainly describes PHP The most basic file writing tutorial in the file read and write operation. "; 
fwrite($fp,$fileData); 
fclose($fp); 
} 
?> 

Note: in this file read-write instance code, the main function is to write two lines of text to the file.
Knowledge:
1. Use the fopen function to open the file. When you use the fopen function to open the file, you first need to specify:
Open the file for what? Do you read data from a file, write data to a file, or read and write data to a file?
You also need to consider whether you want to overwrite the data in the original file or simply add the new data to the end of the file if the data already exists in the file
These problems involve the application of the file mode in the fopen function in the PHP file read and write operation. The prototype of the fopen function is as follows:

fopen(filename,mode,include_path,context) 

When you call the file manipulation function fopen(), you usually pass two or three arguments.
Filename: specifies the file or URL to open. You can specify the absolute path to the file, usually C:\ for Windows and/for Unix. You can also open the remote file with the URL. The file I'm writing to here is in the same directory as the PHP file I'm writing to.
Mode: specifies the type of access required to the file/stream. Open file mode.
Include_path: optional. If you need to search the include_path for files, you can set this parameter to 1 or TRUE.
Commonly used fopen file operation mode description
"R" - open the file read-only, starting with the header.
"R +" - open the file as a read and write.
"W" - open the file as a write, starting with the file header. Try to create the file if it doesn't exist, and delete the contents of the file if it does.
"W +" - open the file as a read and write, starting with the file header. Try to create the file if it doesn't exist, and delete the contents of the file if it does.
"A" - opens as a write, appending from the end of the file. If the file does not exist, try to create it.
"A +" - opens in read-write mode and appends or reads from the end of the file. If the file does not exist, try to create it.
Note: when doing file read and write operations, you must ensure that the open file has the appropriate read and write permissions, otherwise fopen will report an error. You can use @ to suppress errors and then handle them properly.
2, after using the file operation function fopen to open the file, need to assign value to the variable, and then write to the file pointer to $fp, in the above PHP file writing operation tutorial instance, I use a line by line storage, namely, the newline storage, mainly through \n as the newline separator.
Fwrite file writing function prototype is as follows:
 
fwrite(fp,string,length) 

Here you can also use the file to write to the function fputs, which is an alias to fwrite.

Length is optional in fwrite, which 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 length of characters to the specified file. Fwrite () returns the number of characters written to the file, or false for an error.

After the file write operation is completed, you need to close the file handle, otherwise it will occupy system resources, etc. You can do this using the fclose($fp) function. File closure returns true on success, or false on failure.

The file write operation is now complete.

The above is the most basic application of PHP file write operation tutorial file write operation, in addition to the file write operation, in PHP website development often need to read the relevant file content, file write operation function can use different functions to achieve the file read operation. Next time we'll look at how to read the file.

Related articles: