How PHP can tell if a remote picture file exists _php tutorial

Source: Internet
Author: User

fopen () method

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

 
  

The fopen () function opens the file or URL. If the open fails, the function returns FALSE.

Syntax: fopen (Filename,mode,include_path,context)

Parameters Description
FileName Necessary. Specifies the file or URL to open.
Mode Necessary. Specifies the type of access required to the file/stream. The possible values are shown in the following table.
Include_path Optional. If you also need to retrieve the file in Include_path, you can set the parameter to 1 or TRUE.
Context Optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream.

Possible values for the mode parameter:

mode description
"R" read-only opens, pointing the file pointer to the file header.
"r+" read-write mode opens, pointing the file pointer to the file header.
"W" write open, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
"w+" read-write mode opens, pointing the file pointer at the file header and truncating the file size to zero. If the file does not exist, try to create it.
"A" write open, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
"A +" read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it. The
"x" is created and opened as a write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it. This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files.
"x+" is created and opened as read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it. This specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files.

fopen () binds the name resource specified by filename to a stream. If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet been registered for encapsulation, PHP will issue a message to help examine potential problems in the script and continue with filename as a normal file name.

If PHP thinks that filename specifies a local file, it will attempt to open a stream on that file. The file must be accessible by PHP, so you need to confirm that the file access permission allows that access. If Safe mode is activated or open_basedir is applied, further restrictions apply.

If PHP believes that filename specifies a signed protocol, and that the protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is off, PHP will issue a warning, and fopen's call fails.

The use of a fopen

 
  

But if the image resource is on the server, the function waits, so we need a fallback plan.

CURL method

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

 
  

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.

http://www.bkjia.com/PHPjc/752418.html www.bkjia.com true http://www.bkjia.com/PHPjc/752418.html techarticle The simplest method of the fopen () method is to use fopen () to see if the file can be opened, and the file will, of course, exist. Php$url = ' http://www.bkjia.com/images/test.jpg '; if (@fopen ($ URL,...

  • Related Article

    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.