php: exclusive lock on file never obtained -


on php script, after parse string, need write data file (after create if doesn't exist). before write file, need exclusive lock avoid problems. code:

foreach ($elements[0] $current) {     $file_handler = fopen($my_folder . "/" . $current . ".txt", "a");     $locked = flock($file_handler, "lock_ex");     while (!$locked) {         usleep(500000);         $locked = flock($file_handler, "lock_ex");     }     //got lock     fwrite($file_handler, $mystring . "\n");     //release lock     flock($file_handler, lock_un);     fclose($file_handler); } return; 

now, seems not work. fopen create file, code seems go in loop inside while (file created, nothing write inside). what's wrong?

you passing argument locking string instead of constant.

try:

$locked = flock($file_handler, lock_ex); 

notice missing double quotes around lock_ex


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -