PHP checks whether remote images, files, or URLs exist.

Source: Internet
Author: User
In php, we can directly related functions for remote image storage. functions such as curl and fopen can be quickly detected. The following describes several examples, I hope to help you. Example 1? Php judges remote files... in php, we can directly related functions for remote image storage. functions such as curl and fopen can be quickly detected. The following describes several examples, I hope to help you.

Example 1

 

Of course, there are also many other methods with more or less restrictions and defects, such:

(1) use the fopen () function. it must be enabled in allow_url_open state. Otherwise, an error is returned.

$ Url = 'http: // s1.phprm.com/img/php_logo.png'{if (@ fopen ($ url, 'r') {echo 'file exists';} else {echo 'file does not exist ';}

(2) get_headers gets all the headers sent by the server in response to an HTTP request, which is less efficient. you can test it.

 Array ('timeout' => 1,); $ headers = get_headers ($ url); if (preg_match ('/200/', $ headers [0]) {echo 'file exists';} else {echo 'file does not exist';}?>

(3) file_get_contents () function

 Array ('timeout' => 3,); $ context = stream_context_create ($ opts); $ resource = @ file_get_contents ('http: // s1.phprm.com/img/php_logo.png', false, $ context ); if ($ resource) {echo 'file exists';} else {echo 'file does not exist';}?>

Reference: fopen function usage

Parameter description

Filename is required. Specifies the file or URL to open.

Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below.

Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE.

Context is optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream.

Possible value of the mode parameter

Mode description

Open the file in read-only mode and point the file pointer to the file header.

Open the "r +" read/write mode and point the file pointer to the file header.

Open the "w" 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 "w +" 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.

"A" is opened in writing mode, pointing the file pointer to the end of the file. If the file does not exist, try to create it.

Open the "a +" 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


Tutorial address:

Reprinted! But please include the article address ^

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.