Php flock
Contents |
PHP function flock
This function locks or unlock a file.
Syntax flock
- file is a file handle.
- mode: specifies operation of the lock
- LOCK_SH to acquire a shared lock (reader).
- LOCK_EX to acquire an exclusive lock (writer).
- LOCK_UN to release a lock (shared or exclusive).
- LOCK_NB if you don't want flock() to block while locking. (not supported on Windows)
- block sets to true if lock block.
flock(file,mode,block);
Example 1
<?php $filename = 'myfile1.txt'; $fh = fopen($filename, "w+"); // exclusive lock if (flock($fh, LOCK_EX)) { fwrite($fh, "Great Leader"); // unlock flock($fh, LOCK_UN); } else { echo "Error lock!"; } fclose($fd); ?>
Example 2
<?php $filename = 'myfile31.txt'; $fh = fopen($filename, "w+"); if ($fh && flock($fh, LOCK_EX)) { ftruncate($fh, 0); fwrite($fh, 'great leader'); sleep(5); // assume writing for 5 seconds fwrite($fh, 'great mother'); } else { echo "Error lock!"; } fclose($fh); ?>
Related Links
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-- lchgrp-- lchown-- link-- linkinfo-- lstat-- mkdir-- move_uploaded_file-- parse_ini_file-- parse_ini_string-- pathinfo-- pclose-- popen-- readfile-- readlink-- realpath_cache_get-- realpath_cache_size-- realpath-- rename-- rewind-- rmdir-- set_file_buffer-- stat-- symlink-- tempnam-- tmpfile-- touch-- umask-- unlink--