Home Backend Development PHP Tutorial PHP determines whether the remote image can be called

PHP determines whether the remote image can be called

May 21, 2018 am 11:16 AM
php transfer

This time I will bring you PHP to determine whether a remote image can be called. What are the precautions for PHP to determine whether a remote image can be called? The following is a practical case, let's take a look. Note: When making a picture preview, I encountered a problem, which is to determine whether the

remote file

exists (not the same server). Examples, multiple methods to determine whether remote images exist.

<?php
//方法一
function 
file_exists
($url)
{
$ch = curl_init();
curl_setopt($ch, curlopt_url,$url);
curl_setopt($ch, curlopt_nobody, 1); // 不下载
curl_setopt($ch, curlopt_failonerror, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
if(curl_exec($ch)!==false)
return true;
else
return false;
}
//方法二
function file_exists2($url)
{
if(file_get_contents($url,0,null,0,1))
return 1;
else
return 0;
}
//方法三
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
}
Copy after login

Code description:

Method one, returns FALSE regardless of whether the picture is present;

Method two, feasible under windows, returns TRUE regardless of whether the picture is present under LINUX;
Method 3 should be the most appropriate
In addition: There are efficiency issues with the get_headers() method, and it is recommended not to use it as this solution.

fsockopen version:

<?php
$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);
Copy after login

Use

http status code

to determine whether the file exists. For example, responses 302, 301, 404, etc. all mean that it does not exist. If it is 200, 304, etc., it can Treat the file as existing. 1, fopen() method:

<?php
    $url = 'http://www.test.com/images/test.jpg';
      if( @fopen( $url, 'r' ) )
    {
        echo 'File Exits';
    }
    else
    {
        echo 'File Do Not Exits';
    }
    ?>
Copy after login

2, CURL method:
<?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 '文件不存在';
    }
?>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!

Recommended reading:

php array function shuffle() and array_rand() random function usage steps detailed explanation

php array search function usage Summary of methods

The above is the detailed content of PHP determines whether the remote image can be called. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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 Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles