PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I have a program where people can enter in a remote image file address to be later used with imagecreatefrom...().
The problem is that on some servers that images hosted are slow or dont plain work with these functions, therefore making my program act slow.
Is there a timeout where I can say if the function doesn't respond within 5 seconds to skip it and execute some other code or give me a bool for me to check ?
This wouldn't be correct or work but something like..
You would have to make that script run as a shell script and call it from exec() from another script having it run in the background, that function would write something to a file to let it know it "started" and it would write something else to that file when it finished. The calling script would check that file after 5 seconds and it would either find a true or a false (wether or not its done), if its done send a kill command to your other process and then run the other code.
#!/usr/bin/php
// this var would come from a paramater passed to the script on the command line
$file = (int)$file;
$h = fopen('/tmp/status_'.$file);
fwrite($h,'0');
fclose($h);
// do something that takes a long time and save the result into $data
$h = fopen('/tmp/status_'.$file);
fwrite($h,$data);
fclose($h);
$id = 2; // a unique ID which will be the name of the file
exec('./do.php -'.(int)$id);
$data = false;
$i0;
while(!$data && $i <=5) {
$data = file_get_contents('/tmp/status_'.(int)$id);
sleep(1);
$i++;
}
if (!$data) // it did not finish
if ($data) // it did finish
The only thing to do would be to have that call to exec() launch it in the background, I think its as simple as adding a & to the end but I can't be sure. You would also want to capture the PID that is returned to you by the OS so you can kill it if the script does not finish