search
HomeBackend DevelopmentPHP ProblemHow to determine whether a remote image exists in php

How to determine whether a remote image exists in php

php method to determine whether a remote image exists:

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 version:

 $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 class="brush:php;toolbar:false">";
    print_r($headers);

Use http status code to determine whether the file exists , for example, responses 302, 301, 404, etc. all mean that the file does not exist. If it is 200, 304, etc., it can be regarded as the file exists.

fopen() method:

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

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

CURL method:

<?php
    $url2 = &#39;http://www.test.com/test.jpg&#39;;

    $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 &#39;文件不存在&#39;;
    }
    ?>

Recommendation:php server

The above is the detailed content of How to determine whether a remote image exists in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool