In PHP, you can perform basic read/write operations on files, search for and locate file pointers, and lock the files being read.
Advanced applications for PHP file processing-remote file access and file locking
In PHP, you can perform basic read/write operations on files, search for and locate file pointers, and lock the files being read.
The previous articles php file processing-opening/closing a file, php file processing-reading a file (one character, string), php file processing-how to read files and PHP file processing-writing and operating files describes basic read/write operations for file processing. This section describes advanced file processing technologies.
I. remote file access
PHP supports file calling in URL format. you only need to set allow_url_fopen in php. ini and set this option to ON. After restarting the server, you can use the HTTP or ftp url format,
For example:
fopen("http://http://127.0.0.1/php/1.php","rb");
II. file locking
When writing content to a text file, you must first lock the file to prevent other user colleagues from modifying the content of the file. the Function of locking the file in PHP is flock (), the syntax format of this function is as follows:
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )
Parameter value |
Description |
LOCK_SH |
Get Share Lock (read file) |
LOCK_EX |
Get Exclusive lock (Write file) |
LOCK_UN |
Release Lock |
LOCK_NB |
Prevent flock () from being blocked at lock time |
Use the flock () function in the following instance to lock the file, write the data, unlock the file, and close the file. the sample code is as follows:
Output result:
Note:
When writing data to a file, open the file in W or w + mode. if LOCK_EX is used, other users accessing the file at the same time cannot obtain the file size, operation not allowed!
In the next article, we will continue to introduce file pointers of advanced applications for file processing. For more information, see php file processing advanced applications-file pointers.
The above is the advanced application of PHP file processing-detailed content of remote file access and object locking. For more information, see other related articles in the first PHP community!