Question

In PHP, I want to put a number of second delay on each iteration of the loop.

for ($i=0; $i <= 10; $i++) {
    $file_exists=file_exists($location.$filename);
    if($file_exists) {
        break;
    }

    //sleep for 3 seconds
}

How can I do this?

Was it helpful?

Solution

Use PHP sleep() function. http://php.net/manual/en/function.sleep.php This stops execution of next loop for the given number of seconds. So something like this

for ($i=0; $i <= 10; $i++) {
    $file_exists=file_exists($location.$filename);
    if($file_exists) {
        break;
    }
    sleep(3); // this should halt for 3 seconds for every loop
}

OTHER TIPS

I see what you are doing... your delaying a script to constantly check for a file on the filesystem (one that is being uploaded or being written by another script I assume). This is a BAD way to do it.

  1. Your script will run slowly. Choking the server if several users are running that script.
  2. Your server may timeout for some users.
  3. HDD access is a costly resource.
  4. There are better ways to do this.

You could use Ajax. And use a timeout to call your PHP script every few seconds. This will avoid the slow script loading. And also you can keep doing it constantly (the current for loop will only run for 33 seconds and then stop).

You can use a database. In some cases database access is faster than HDD access. Especially with views and caching. The script creating the file/uploading the file can set a flag in a table (i.e. file_exists) and then you can have a script that checks that field in your database.

You can use sleep(3) which sleeps the thread for 3 seconds.

Correction sleep method in php are in seconds.

Hare are two ways to sleep php script for some period of time. When you have your code and want to pause script working for some time use these functions. In these examples the first part of code will be done on script run and the second part of code will be done but with time delay.

  1. Using sleep() function you can define sleep time in seconds.

Example:

echo "Message 1";
// The first part of code.
$timeInSeconds = 3;
sleep($timeInSeconds);
// The second part of code.
echo "Message 2";

This way it is possible to sleep php script for 3 seconds. Using this function you can sleep script for whole number (integer) of seconds.

  1. Using usleep() function you can define sleep time in microseconds. This sleep time is convenient for intervals that require more precise time than one second.

Example:

echo "Message 1";
// The first part of code.
$timeInMicroSeconds = 2487147;
usleep($timeInMicroSeconds);
// The second part of code.
echo "Message 2";

You can use this function if you want to sleep php for smaller time values than second (float). In this example I have put script to sleep for 2.487147 seconds.

Have you considered using a PHP Daemon script using supervisorD. I use it in multiple tasks that are required to be running all the time.

The catch is making sure that each time you are running your script you check for memory resources. If its too high, stop the process and then let it restart itself up again.

I have successfully used this process to be always checking database records for tasks to process.

It might be overkill but worth considering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top