Advanced applications for PHP file processing-remote file access and file locking

Source: Internet
Author: User
Tags flock
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!

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.