Files
php_8/files/lock_ex.php
2022-05-08 08:56:58 +03:00

18 lines
686 B
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
$file = 'file.txt';
// Вначале создаем пустой файл, ЕСЛИ ЕГО ЕЩЕ НЕТ.
// Если же файл существует, это его не разрушит.
fclose(fopen($file, 'a+b'));
// Блокируем файл
$f = fopen($file, 'r+b') or die('Не могу открыть файл!');
flock($f, LOCK_EX); // ждем, пока мы не станем единственными
// . . .
// В этой точке мы можем быть уверены, что только эта
// программа работает с файлом
// . . .
// Все сделано. Снимаем блокировку.
fclose($f);