PHP5 Manual Free Open Book

PHP5 Manual

PHP Manual
PrevNext

flock

(PHP 3 >= 3.0.7, PHP 4, PHP 5)

flock -- Portable advisory file locking

Description

bool flock ( resource handle, int operation [, int &wouldblock] )

PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work).

Note: flock() is mandatory under Windows.

flock() operates on handle which must be an open file pointer. operation is one of the following values:

  • To acquire a shared lock (reader), set operation to LOCK_SH (set to 1 prior to PHP 4.0.1).

  • To acquire an exclusive lock (writer), set operation to LOCK_EX (set to 2 prior to PHP 4.0.1).

  • To release a lock (shared or exclusive), set operation to LOCK_UN (set to 3 prior to PHP 4.0.1).

  • If you don't want flock() to block while locking, add LOCK_NB (4 prior to PHP 4.0.1) to operation.

flock() allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows). The optional third argument is set to TRUE if the lock would block (EWOULDBLOCK errno condition). The lock is released also by fclose() (which is also called automatically when script finished).

Returns TRUE on success or FALSE on failure.

Example 1. flock() example

<?php

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

if (
flock($fp, LOCK_EX)) { // do an exclusive lock
    
fwrite($fp, "Write something here\n");
    
flock($fp, LOCK_UN); // release the lock
} else {
    echo
"Couldn't lock the file !";
}

fclose($fp);

?>

Note: Because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()).

Warning

flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details.

On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!

flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users).


     Main Menu
PHP Manual
Table of Contents
Preface
Getting Started
Language Reference
Security
Features
Function Reference
Apache-specific Functions
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Classkit Functions
Class/Object Functions
COM and .Net (Windows)
ClibPDF Functions
Crack Functions
Character Type Functions
CURL, Client URL Library Functions
Cybercash Payment Functions
Cyrus IMAP administration Functions
Date and Time Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
DB++ Functions
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
.NET Functions
Error Handling and Logging Functions
Program Execution Functions
Exif Functions
File Alteration Monitor Functions
FrontBase Functions
Forms Data Format Functions
filePro Functions
Filesystem Functions
basename
chgrp
chmod
chown
clearstatcache
copy
delete
dirname
disk_free_space
disk_total_space
diskfreespace
fclose
feof
fflush
fgetc
fgetcsv
fgets
fgetss
file_exists
file_get_contents
file_put_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
flock
fnmatch
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
link
linkinfo
lstat
mkdir
move_uploaded_file
parse_ini_file
pathinfo
pclose
popen
readfile
readlink
realpath
rename
rewind
rmdir
set_file_buffer
stat
symlink
tempnam
tmpfile
touch
umask
unlink
FriBiDi Functions
FTP Functions
Function Handling Functions
Gettext
GMP Functions
HTTP Functions
Hyperwave Functions
Hyperwave API Functions
Firebird/InterBase Functions
IBM DB2, Cloudscape and Apache Derby Functions
ICAP Functions [deprecated]
iconv Functions
ID3 Functions
Informix Functions
IIS Administration Functions
Image Functions
IMAP, POP3 and NNTP Functions
PHP Options&Information
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
LDAP Functions
libxml Functions
LZF Functions
Mail Functions
mailparse Functions
Mathematical Functions
MaxDB PHP Extension
Multibyte String Functions
MCAL Functions
Mcrypt Encryption Functions
MCVE Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Mohawk Software Session Handler Functions
mSQL Functions
Microsoft SQL Server Functions
muscat Functions
MySQL Functions
Improved MySQL Extension
Ncurses Terminal Screen Control Functions
Network Functions
YP/NIS Functions
Lotus Notes Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Oracle 8 functions
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions
Output Control Functions
Object property and method call overloading
Ovrimos SQL Functions
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF functions
PDO Functions
Verisign Payflow Pro Functions
PostgreSQL Functions
POSIX Functions
Printer Functions
Pspell Functions
qtdom Functions
Rar Functions
GNU Readline
GNU Recode Functions
Regular Expression Functions (POSIX Extended)
Semaphore, Shared Memory and IPC Functions
SESAM Database Functions
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
Secure Shell2 Functions
Stream Functions
String Functions
Shockwave Flash Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
ODBC Functions (Unified)
URL Functions
Variable Handling Functions
vpopmail Functions
W32api Functions
WDDX Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XSL functions
XSLT Functions
YAZ Functions
Zip File Functions (Read Only Access)
Zlib Compression Functions
Zend API
PHP API: Interfaces for extension writers
FAQ: Frequently Asked Questions
Appendixes
Copyright


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele