search
Homephp教程php手册PHP 如何使用文件锁

PHP 如何使用文件锁

Jun 13, 2016 am 10:40 AM
endflockfopenfromlockphpthewriteusehowdocumentLock

//WRITE
$fp = fopen("test.txt", ab);              //From the end
flock($fp, LOCK_EX);                        //lock the file for waiting...
fwrite($fp, Just A Test String.......);   //Start writing...
flock($fp, LOCK_UN);                        //Release write lock
fclose($fp);                                //Close the file读操作:

//READ
$fp = fopen("test.txt", r);
flock($fp, LOCK_SH);
//Read from the file.......
flock($fp, LOCK_UN);
fclose($fp);来详细的看看 PHP 手册上面关于函数 flock 的介绍吧:

flock -- 轻便的咨询文件锁定

其函数原型为:bool flock ( int handle, int operation [, int &wouldblock] )

PHP 支持以咨询方式(也就是说所有访问程序必须使用同一方式锁定, 否则它不会工作)锁定全部文件的一种轻便方法,需要注意的地方是:在 Windows 下 flock() 将会强制执行。flock() 操作的 handle 必须是一个已经打开的文件指针。operation 可以是以下值之一:

要取得共享锁定(读取的程序),将 operation 设为 LOCK_SH(PHP 4.0.1 以前的版本设置为 1)。

要取得独占锁定(写入的程序),将 operation 设为 LOCK_EX(PHP 4.0.1 以前的版本中设置为 2)。

要释放锁定(无论共享或独占),将 operation 设为 LOCK_UN(PHP 4.0.1 以前的版本中设置为 3)。

如果不希望 flock() 在锁定时堵塞,则给 operation 加上 LOCK_NB(PHP 4.0.1 以前的版本中设置为 4)。 flock() 允许执行一个简单的可以在任何平台中使用的读取/写入模型(包括大部分的 Unix 派生版和甚至是 Windows)。如果锁定会堵塞的话(EWOULDBLOCK 错误码情况下),可选的第三个参数会被设置为 TRUE。锁定操作也可以被 fclose() 释放(代码执行完毕时也会自动调用)。如果成功则返回 TRUE,失败则返回 FALSE。

来看看 PHP manual 上面经典的实例:

$fp = fopen("/tmp/lock.txt", "w+");

if (flock($fp, LOCK_EX)) { // 进行排它型锁定
    fwrite($fp, "Write something here ");
    flock($fp, LOCK_UN); // 释放锁定
} else {
    echo "Couldnt lock the file !";
}

fclose($fp);

?> 注意: 由于 flock() 需要一个文件指针, 因此可能不得不用一个特殊的锁定文件来保护打算通过写模式打开的文件的访问(在 fopen() 函数中加入 "w" 或 "w+")。

注意:

flock() 不能用于 NFS 以及其它一些网络文件系统。详细资料查看自己操作系统的文档。

在部分操作系统中 flock() 以进程级实现。当用一个多线程服务器 API(比如 ISAPI)时,可能不可以依靠 flock() 来保护文件,因为运行于同一服务器实例中其它并行线程的 PHP 脚本可以对该文件进行处理。

flock() 不支持旧的文件系统,如 FAT 以及它的派生系统。因此,此环境下总是返回 FALSE(尤其是对 Windows

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment