PHP to determine whether remote pictures exist in several ways

Source: Internet
Author: User
Tags curl response code

This article mainly introduced the PHP to judge whether the remote pictures exist several methods, the need for friends can refer to the

In doing a picture preview of the things, encountered a problem, is to determine whether the remote file exists (not the same server).

The code is as follows:

The code is as follows:

Method One

function file_exists ($url)

{

$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, $url);

curl_setopt ($ch, curlopt_nobody, 1); Do not download

curl_setopt ($ch, Curlopt_failonerror, 1);

curl_setopt ($ch, Curlopt_returntransfer, 1);

if (curl_exec ($ch)!==false)

return true;

Else

return false;

}

Method Two

function File_exists2 ($url)

{

if (file_get_contents ($url, 0,null,0,1))

return 1;

Else

return 0;

}

Method Three

function File_exists ($url) {

$curl = Curl_init ($url);

Do not retrieve data

curl_setopt ($curl, Curlopt_nobody, true);

Send Request

$result = curl_exec ($curl);

$found = false;

If the request did not send a failure

if ($result!== false) {

and check to see if the HTTP response code is 200.

}

Method one returns false regardless of whether the picture is in or out;

Method Two windows is feasible under Linux, regardless of whether the picture is not in the return to true;

Method Three should be the most appropriate

In addition: there is an efficiency problem with the Get_headers () method, which is not recommended for use as this solution

Fsockopen version:

The code is as follows:

$url = "Baidu_sylogo1.gif";

$info = Parse_url ($url);

$fp = Fsockopen ($info [' Host '], $errno, $ERRSTR, 30);

Fputs ($fp, "get {$info [' path ']} http/1.1rn");

Fputs ($FP, "host: {$info [' host ']}rn");

Fputs ($fp, "connection:closernrn");

$headers = Array ();

while (!feof ($fp)) {

$line = fgets ($FP);

if ($line!= "rn") {

$headers [] = $line;

}else {

Break

}

}

echo "

";

Print_r ($headers);

The HTTP status code is used to determine whether a file exists, for example, Response 302,301,404 is non-existent, if 200,304, etc. can be considered as a file existence.

fopen () Method:

The code is as follows:

$url = ' test.jpg ';

if (@fopen ($url, ' R '))

{

Echo ' File Exits ';

}

Else

{

Echo ' File does not Exits ';

}

?>

Curl Method:

The code is as follows:

$url 2 = ' test.jpg ';

$ch = Curl_init ();

$timeout = 10;

curl_setopt ($ch, Curlopt_url, $url 2);

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 ' file does not exist ';

}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.