我有这个url www.example.com/1.png
。我想运行一个循环,如下所示:
for ($i=0; $i<=10; $i++){
$url = 'www.example.com/'.$i.'.png';
}
现在我有10倍的urls。假设10个urls中有7个是图像,其他3个不可用。我想从每个网址下载图像,如果网址不存在,那么它将不会下载任何东西。
下面是同样的事情,但我认为更基本的解释是:
www.example.com/1.png --- url exists, so i download the image and save it in my folder.
www.example.com/2.png --- url exists, so i download the image and save it in my folder.
www.example.com/3.png --- url doesnt exist, so i dont download anything
www.example.com/4.png --- url exists, so i download the image and save it in my folder.
www.example.com/5.png --- url exists, so i download the image and save it in my folder.
www.example.com/6.png --- url exists, so i download the image and save it in my folder.
www.example.com/7.png --- url doesnt exist, so i dont download anything
www.example.com/8.png --- url exists, so i download the image and save it in my folder.
www.example.com/9.png --- url exists, so i download the image and save it in my folder.
www.example.com/10.png --- url doesnt exist, so i dont download anything
很抱歉我的英语不好,有什么建议我可以解决这个问题吗?
发布于 2013-03-19 21:20:52
使用file_get_contents()
下载图像将生成一个$http_response_header
变量,该变量包含您可以检查的HTTP响应头。请参阅documentation here
发布于 2013-03-19 21:19:23
您可以检查报头中是否有404错误。
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
//Does not exist
}
else
{
//Exists, so put download code here
}
发布于 2018-03-29 08:05:37
您可以做的是在一个多curl请求中将它们全部排队,然后过滤掉那些没有返回值< 399的请求。这样一来,您就可以让curl一次请求所有的请求,而不是一次请求一个。还要为此操作应花费的时间设置一个超时,例如5秒。
https://stackoverflow.com/questions/15500571
复制相似问题