PHP sleep delay

前端 未结 5 706
心在旅途
心在旅途 2020-12-14 06:52

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.$filenam         


        
5条回答
  •  萌比男神i
    2020-12-14 07:25

    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.

提交回复
热议问题