Hi I have a php function it takes about 20 min (or more) to run and finish.
but if somebody else run it during last run...The function break with unwanted result.
this is the structures:
function myfunc()
{
foreach()
{
//do somthing
}
}
I think I can do it via a file like this:
I created a text file ... if content of that text was '1',the function is lock. if not the function is free.
function myfunc()
{
$file = "lock.txt";
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);
if($line == 0)
file_put_contents($file, "1");
else
return false;
foreach()
{
}
//after finish for each
file_put_contents($file, "0"); // free function for other user
}
I think logically it must be true ...but do not work! after first run, the content of lock.txt file remain 1. (not change to 0 after finish)
I think maybe the function breaks during process becuase of long times! because I handle all break state in function. can tel me how can handle this problem? how can I be sure that lock.txt will be '0' after finish or break function?
Well there is already a function to implement lock Called flock()
using a flag LOCK_SH you can lock it