PHP filter does not exist picture resources, _php tutorial

Source: Internet
Author: User

PHP filter does not exist in the picture resources,


Method One:

The simplest way is to use fopen (), to see if the file can be opened, can hit the file of course exists.

<?php$url = ' http://www.bkjia.com/images/test.jpg ', if (@fopen ($url, ' R ')) {  echo ' File Exits ';} else {echo ' Fil e do not Exits ';}? >

Method Two:

/**    * Filter for non-existent picture resources    *    * @author wanggeng 
 
  
   
      * @return vodi * */      private static function _ Checkall ($url)   {      $curl = Curl_init ($url);     curl_setopt ($curl, Curlopt_nobody, true);     $result = false;     $res = curl_exec ($curl);     if ($res!== false) {       $statusCode = Curl_getinfo ($curl, curlinfo_http_code);       if ($statusCode = =) {         $result = true;       }     }     Curl_close ($curl);     return $result;   } 
 
  

First, create a link to a curl that is linked to the execution URL, which is the picture or file.
Initial one variable is false
or open the linked Head header information each HTTP request will have an HTTP Code
We're going to verify this code.
If the return code is 200 to prove that the resource exists to a true value before the variable is not assigned the value

Method Three:

CURL method

Curl is a very useful class library, see below How to use it to judge.

<?php$url2 = ' http://www.bkjia.com/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 ';}? >

Curl_exec () If the file does not exist after execution, the following information is returned:

http/1.1 404 Not Founddate:tue, 05:08:34 Gmtserver:apacheaccept-ranges:bytescontent-length:354content-type : text/html

Use the regular to see if there are 404, and some files do not exist.

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/991803.html www.bkjia.com true http://www.bkjia.com/PHPjc/991803.html techarticle PHP Filter does not exist image resources, method one: The simplest way is to use fopen (), to see if the file can be opened, the file will certainly exist. Php$url = ' http://www.bkjia.com/ ...

  • 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.