【发布时间】:2014-02-21 07:46:01
【问题描述】:
我有一个脚本,它基本上是在清理东西。
脚本的一部分需要检查图像是否存在,使用file_get_contents
我知道这会遇到问题,我会不时收到Fatal error: Maximum execution time of 30 seconds exceeded 并希望避免这种情况。
有没有办法设置一个开始计数的计数器,如果 file_get_contents 在 25 秒后失败,脚本会忽略然后继续。
我会说,我知道我可以,但我不想增加时间限制。
这是基本脚本:
$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
}
}
【问题讨论】:
-
参见:[Timeout a function in PHP][1] [1]: stackoverflow.com/questions/10587323/timeout-a-function-in-php
-
好吧,如果最大执行时间到期,那么脚本将终止,它不是一个循环,我们可以在 25 秒后刷新页面并使用参数作为最后一个索引重新加载页面,循环可以重新开始索引。对于文件操作,我不确定它是否可以完成,但是您可以增加最大执行时间。
-
如果您只需要检查图像是否存在,则只需使用 file_exists() 函数而不是 file_get_contents()
标签: php