类别:PHP问题 / 日期:2019-11-26 / 浏览:195 / 评论:0

php推断长途图片是不是存在的要领:

function file_exists($url) {
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
// 发送要求
$result = curl_exec($curl);
$found = false;
// 假如要求没有发送失利
if ($result !== false) {
// 再搜检http相应码是不是为200
}

fsockopen版:

 $url = "http://www.baidu.com/img/baidu_sylogo1.gif";

    $info = parse_url($url);
    $fp = fsockopen($info['host'], 80,$errno, $errstr, 30);
    fputs($fp,"GET {$info['path']} HTTP/1.1\r\n");
    fputs($fp, "Host: {$info['host']}\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    $headers = array();
    while(!feof($fp)) {
    $line = fgets($fp);
    if($line != "\r\n") {
    $headers[] = $line;
    }else {
    break;
    }
    }

    echo "<pre>";
    print_r($headers);

经由过程http状况码来推断文件是不是存在,比方,相应 302,301,404等都为不存在,假如是200,304,等能够视为文件存在。

fopen()要领:

<?php
    $url = 'http://www.test.com/images/test.jpg';

    if( @fopen( $url, 'r' ) )
    {
        echo 'File Exits';
    }
    else
    {
        echo 'File Do Not Exits';
    }
    ?>

CURL要领:

<?php
    $url2 = 'http://www.test.com/test.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);
    //echo $contents;
    if (preg_match("/404/", $contents)){
        echo '文件不存在';
    }
    ?>

引荐:php服务器

以上就是php怎样推断长途图片是不是存在的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章