PHP: how to determine whether a remote image file exists _ PHP Tutorial

Source: Internet
Author: User
PHP determines whether a remote image file exists. The simplest method of the fopen () method is to use fopen () to check whether the file can be opened. if the file can be opened, the file will certainly exist .? Php?urlwww.bkjia.comimagestest.jpg; if (@ fopen ($ url, fopen () method

The simplest method is to use fopen () to check whether the file can be opened. if the file can be opened, the file exists.

 

The fopen () function opens a file or URL. If it fails to be opened, the function returns FALSE.

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

Parameters Description
Filename Required. Specifies the file or URL to open.
Mode Required. Specifies the access type of the file/stream. Possible values are shown in the table below.
Include_path Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE.
Context Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream.

Possible values of the mode parameter:

Mode Description
"R" Open the file in read-only mode and point the file pointer to the file header.
"R +" Open in read/write mode and point the file pointer to the file header.
"W" Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
"W +" Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
"" Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
"A +" Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
"X" Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
"X +" Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

Fopen () binds the name resource specified by filename to a stream. If filename is in the format of "scheme: //...", it is treated as a URL. PHP will use the search protocol processor (also known as the Encapsulation Protocol) to process this mode. If the protocol has not yet registered the Encapsulation Protocol, PHP will send a message to help check the potential problems in the script and continue executing the filename as a normal file name.

If PHP considers filename to be a local file, it will try to open a stream on the file. This file must be accessible by PHP. Therefore, you need to confirm that the file access permission permits this access. If the security mode or open_basedir is activated, further restrictions are applied.

If PHP considers filename to be a registered protocol registered as a web URL, PHP checks and confirms that allow_url_fopen is activated. If it is disabled, PHP will issue a warning, while the fopen call will fail.

Usage of one fopen

 

However, if the server where the image resource is located crashes, this function will always wait, so we need an alternative solution.

CURL method

CURL is a very useful class library. let's take a look at how to use it to determine.

 

After curl_exec () is executed, if the file does not exist, the following information is returned:

HTTP/1.1 404 Not FoundDate: Tue, 14 Feb 2012 05:08:34 GMTServer: ApacheAccept-Ranges: bytesContent-Length: 354Content-Type: text/html

Use regular expressions to check whether 404 exists. If yes, the file does not exist.

Http://www.bkjia.com/PHPjc/752418.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752418.htmlTechArticlefopen () method is the simplest way is to use fopen (), to see whether the file can be opened, can play on the file of course there is. ? Php $ url = 'http: // www.bkjia.com/images/test.jpg'{if (@ fopen ($ url ,...

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.