加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP判断远程图片文件是不是存在

发布时间:2023-06-01 12:39:12 所属栏目:PHP教程 来源:
导读:在php中我们利用file_exists来判断本地的文件是否存在,那么如何用PHP判断远程文件是否存在呢,下在我们一起来看一个例子,希望此例子是你需要用到的,代码如下:<?php /* *用PHP判断远程图片(文件)是否存在 *http://ww

在php中我们利用file_exists来判断本地的文件是否存在,那么如何用PHP判断远程文件是否存在呢,下在我们一起来看一个例子,希望此例子是你需要用到的,代码如下:

<?php 

/* 

*用PHP判断远程图片(文件)是否存在 

*http://www.Cuoxin.com 

*/ 

function check_remote_file_exists($url) { 

    $curl = curl_init($url); 

// 不取回数据 

    curl_setopt($curl, CURLOPT_NOBODY, true); 

// 抓取跳转后的内容 

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); 

// 发送请求 

    $result = curl_exec($curl); 

    $found = false; 

// 如果请求没有发送失败 

    if ($result !== false) { 

// 再检查http响应码是否为200 

        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

        var_dump($statusCode); 

        if ($statusCode == 200) {     

// $retcode >= 400 -> not found, $retcode = 200, found. 

            $found = true; 

        } 

    } 

    curl_close($curl); 

    return $found; 

$exists = check_remote_file_exists('http://www.Cuoxin.com /allimg/090403/140941513J2-2.jpg'); 

if ($exists) { 

    echo '存在'; 

} else { 

    echo '不存在'; 

$exists = check_remote_file_exists('http://www.Cuoxin.com /allimg/090403/140941513J2-4.jpg'); 

if ($exists) { 

    echo '存在'; 

} else { 

    echo '不存在'; 

exit; 

?> 

还有一种简单的方法,但效率是低下的,代码如下:

strstr(current(get_headers($url)), "200") 

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!