
php curl判断一个远程文件在不在
2个回答
展开全部
可以使用 curl_error() 函数来判断:
<?php
// 创建一个指向一个不存在的位置的cURL句柄
$url = "http://php.net/aa.txt";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch) === false)
{
echo '文件不存在' . curl_error($ch);
}
// 关闭句柄
curl_close($ch);
2016-12-18 · 知道合伙人互联网行家
关注

展开全部
<?php
$url2 ="http://www.XXX.com/9g.jpg";
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
$contents = substr($contents,0,30);
if (preg_match("/404/", $contents)){
echo '不存在';
}
?>
$url2 ="http://www.XXX.com/9g.jpg";
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
$contents = substr($contents,0,30);
if (preg_match("/404/", $contents)){
echo '不存在';
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询