开发者

Using filesize() PHP function in a Loop - Returns The Same Size

开发者 https://www.devze.com 2023-04-10 17:23 出处:网络
I\'ve done a little bit of PHP coding and am familiar with aspects of it. I have made a PHP script that runs as a cron job that will pull data from a database and if certain conditions are met, some

I've done a little bit of PHP coding and am familiar with aspects of it.

I have made a PHP script that runs as a cron job that will pull data from a database and if certain conditions are met, some information is written to a file.

Because there may be more than one result in the database, a loop is done to run through each result in the database.

Within that loop, I have another loop which will write data to a file. A cron job is then used to call this file every minute and run the contents in the bash script.

So, the PHP loop it setup to see if the file has anything written to it by using the filesize() function. If the filesize is not zero, then it will sleep for 10 seconds and try to read it again. Here is the code:

while(filesize('/home/cron-script.sh') != 0)
{
    sleep(10);
}

Unfortunately, when the filesize is ran, it seems to place some kind o开发者_如何学JAVAf lock or something on the file. The cron job can execute the bash script without a problem and the very last command in the script is to zero out the file:

cat /dev/null > /home/cron-script.sh

But, it seems that once the while loop above is started, it locks in the original file size. As an example, I just simply put in the word "exit" in the cron-script.sh file and then ran through a test script:

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
}

The loop is infinite and will continue to show "filesize: 4" when I put in the word "exit". I will then issue the command at the terminal:

cat /dev/null > /home/cron-script.sh 

Which will then clear the file while I have the test script above running. But, it continues to say the filesize is 4 and never returns to 0 - therefore making the PHP script run until the execution time limit is reached.

Could anyone give me some advice on how I can resolve this issue? In essence, I just need some way to reading the filesize - and if there is any kind of data in the file, it will need to loop through a sleep routine until the file is cleared. The file should clear within one minute (since the cron job calls that cron-script.sh file every minute).

Thank you!


From http://www.php.net/manual/en/function.filesize.php

Note: The results of this function are cached. See clearstatcache() for more details.

To resolve this, remember to call clearstatcache() before calling filesize():

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
    clearstatcache();
}


The results of filesize are cached.

You can use clearstatchace to clear the cache on each iteration of the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号