StudioTime
StudioTime

Reputation: 23989

Avoid maximum execution time with a count - PHP

I have a script which is basically cleaning things up.

One part of the script requires checking if images exist, using file_get_contents

I KNOW this is going to run into problems and I'm going to get Fatal error: Maximum execution time of 30 seconds exceeded from time to time and want to avoid that.

Is there a way to set a counter that starts counting and if file_get_contents fails after say 25 seconds the script ignores then carries on.

I will say, I know I can but I do not want to increase the time limit.

This is the basic script:

$query = "select table_id, image_url from table";

$res = $mysqli->query($query) or trigger_error($mysqli->error."[$query]");

while($row = $res->fetch_array()){

    // save the image
    $img = '/path/to/'.$row[table_id].'.jpg';

    //## need to start counting to 25 secs here

    $saveImage = file_put_contents($img, file_get_contents($row[image_ur]));

    //## check if 25 seconds realised, if so with no $saveImage then continue

    if($saveImage){
        // do something else
    }

}

Upvotes: 0

Views: 677

Answers (2)

Daniel W.
Daniel W.

Reputation: 32300

Instead of fetching the whole file, you could just "ping" it using CURL, so you only fetch its headers and get the status code (200 = file exists, 404 = file does not exist).

If the files are NOT remote, use file_exists(). It would also be interesting wether PHPs wrappers are included into file_exists so you can do:

file_exists('http://image.url.com/file.jpg');

I'm not sure wether this works or does check only for status code but it's worth a try.

Otherwise use CURL with the option not to download the body:

curl_setopt($curl, CURLOPT_NOBODY, true);

Its also good to run this script in CLI rather than through the browser then set the timeout to 2 hours and let it run..

If you can't change the timeout, have a look at Gearman, and simply dispatch your job after hitting your script with the browser.

Update

You don't have to "count to 25", you can set this timeout with options:

CURL: http://php.net/manual/en/function.curl-setopt.php - CURLOPT_CONNECTTIMEOUT

string file_get_contents ( string $filename
      [, bool $use_include_path = false
      [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

Use $context:

resource stream_context_create ([ array $options [, array $params ]] )

In combination with stream_set_timeout:

bool stream_set_timeout ( resource $stream , int $seconds [, int $microseconds = 0 ] )

I strongly suggest to use CURL with the options NOBODY and TIMEOUT so your script will run 10x faster AND have set a timeout (25 is way too much, use 5 or something lower).

CURL also uses Keep-Alive, file_get_contents doesnt.

Upvotes: 1

Viktor Todorov
Viktor Todorov

Reputation: 82

If you really want to do that you can use php micrtotime or time function to determine how long is the script running if you need to terminate the script after 25 seconds. This are the documentation entries for them: http://php.net/time and http://php.net/microtime

Upvotes: 0

Related Questions