فункциون flock() PHP
تعریف و استفاده
فункциون flock() قفل یا آزاد کردن فایلها را انجام میدهد.
اگر موفق شود، true برمیگرداند. اگر شکست بخورد، false برمیگرداند.
دستور زبان
flock(file,lock,بلاک)
پارامتر | توضیح |
---|---|
file | ضروری است. فایلهای باز را که باید قفل یا آزاد شوند را تعیین میکند. |
lock | ضروری است. نوع قفل مورد استفاده را تعیین میکند. |
بلاک | اختیاری است. اگر به 1 یا true تنظیم شود، در صورت قفل شدن از سوی فرآیندهای دیگر جلوگیری میشود. |
Description
The operation of file It must be an already opened file pointer.
lock The parameter can be one of the following values:
- To obtain a shared lock (reading program), set lock to LOCK_SH (set to 1 in versions prior to PHP 4.0.1).
- To obtain an exclusive lock (writing program), set lock to LOCK_EX (set to 2 in versions prior to PHP 4.0.1).
- To release the lock (whether shared or exclusive), set lock to LOCK_UN (set to 3 in versions prior to PHP 4.0.1).
- If you do not want flock() to block during locking, set lock Add LOCK_NB (set to 4 in versions prior to PHP 4.0.1).
Tips and Comments
Tip:Can be used by fclose() To release the locking operation, the code will also automatically call when the code execution is completed.
Note:Since flock() requires a file pointer, it may be necessary to use a special locking file to protect the access to the file intended to be opened in write mode (add "w" or "w+" in the fopen() function).
Example
<?php $file = fopen("test.txt","w+"); // exclusive locking if (flock($file,LOCK_EX)) { fwrite($file,"Write something"); // release lock flock($file,LOCK_UN); } else { echo "Error locking file!"; } fclose($file); ?>