• php判断远程图片或文件是否存在


    1.curl判断远程图片或文件是否存在

    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;
    }

    2.使用fopen()函数,它要在allow_url_open开启的状态下,否则会报错。


    $url = 'http://www.phpddt.com/img/qrcode_for_phpddt.JPG';
    if(@fopen($url, 'r')) {
        echo '文件存在';
    } else {
        echo '文件不存在';
    }

    3.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 '文件不存在';
    }

    4.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 '文件不存在';
    }
  • 相关阅读:
    .NET Core自定义TagHelper和使用Serilog
    .NET Core CSRF
    jq 获取表单全部数据
    Webuploader 简单图片上传 支持多图上传
    CF-1451 E Bitwise Queries 异或 交互题
    CF-1440C2 Binary Table (Hard Version) (构造,模拟)
    CF-1445 C
    ACM模板_axiomofchoice_extra
    Codeforces Round #678 (Div. 2) 题解 (A-E)
    Oracle数据同步
  • 原文地址:https://www.cnblogs.com/cczhao/p/5593685.html
Copyright © 2020-2023  润新知