I have an application that create many php process to read file. Sometimes process read the same file so I need to lock file when a process read It.
The application need to read only one times a file and after delete It. Sometimes two process read the same file and this is a big problem.
I have create many process that every one loop through a folder, read all file, and delete it. I don't want that two process read the same file, but the problem is that the process works simultaneously.
code of every process :
public function readAllXmlAndSave()
$xml_files = glob(TMP . 'xml/*.xml');
foreach($xml_files as $fileXml){
$explStr = explode('/', $fileXml);
$filename = $explStr[count($explStr) - 1];
$path = TMP . '/xml/' . $filename;
$file = new File($path, false);
if($file->exists()){
$string = $file->read();
//some work
$file->close();
$file->delete();
}
}
}
I have create a simple script to understand how lock file works into cakephp 3 but seems that property lock
doesn't work as I aspected.
code
$path = TMP . '/xml/test.xml'; //inside there is "HELLO"
$file = new File($path, false);
$string = $file->read();
echo($string);
$file->lock = true;
$string2 = $file->read();
echo($string2);
$string3 = $file->read();
echo($string3);
I aspected that only the first echo
work not the other.. Why I get always:
HELLO
HELLO
HELLO
and not only the first echo?
HELLO