php判断图片地址,PHP判断远程图片或文件或url是否存在-180

我一般使用curl判断判断远程图片或文件是否存在:

/**

* @link http://www.phpddt.com

*/

function url_exists($url) {

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);

//不下载

curl_setopt($ch, CURLOPT_NOBODY, 1);

//设置超时

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);

curl_setopt($ch, CURLOPT_TIMEOUT, 3);

curl_exec($ch);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($http_code == 200) {

return true;

}

return false;

}

固然也有不少其它方法,或多或少有些限制和缺陷,如:

(1)使用fopen()函数,它要在allow_url_open开启的状态下,不然会报错。

$url = 'http://www.phpddt.com/img/qrcode_for_phpddt.JPG';

if(@fopen($url, 'r')) {

echo '文件存在';

} else {

echo '文件不存在';

}

(2)get_headers取得服务器响应一个 HTTP 请求所发送的全部标头,效率较低,你能够测试下。

$url = 'http://www.phpddt.com/img/qrcode_for_phpddt.JPG';

stream_context_set_default(

array(

'http' => array(

'timeout' => 1,

)

)

);

$headers = get_headers($url);

if(preg_match('/200/',$headers[0])) {

echo '文件存在';

} else {

echo '文件不存在';

}

(3)file_get_contents()函数

$opts = array(

'http'=>array(

'timeout'=>3,

)

);

$context = stream_context_create($opts);

$resource = @file_get_contents('http://www.phpddt.com/img/qrcode_for_phpddt.JPG', false, $context);

if($resource) {

echo '文件存在';

} else {

echo '文件不存在';

}php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值