PHP sleep delay PHP sleep delay php php

PHP sleep delay


Use PHP sleep() function. http://php.net/manual/en/function.sleep.phpThis 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}


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.