PHP implementation code for determining the existence of a remote image or file

  • 2021-01-14 05:42:52
  • OfStack

The easiest way to do this is to use fopen() and see if the file can be opened. If it can be typewritten, it certainly exists


<?php
$url = 'https://www.ofstack.com/images/test.jpg';
if( @fopen( $url, 'r' ) ) 
{ 
    echo 'File Exits';
} 
else 
{
    echo 'File Do Not Exits';
}
?>

Grammar: fopen (filename, mode include_path, context)

parameter describe filename A necessity. Specifies the file or URL to open. mode A necessity. Specifies the type of access required to the file/stream. The possible values are shown in the table below. include_path Optional. If you also need to retrieve files in include_path, you can set this parameter to 1 or TRUE. context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream.

Possible values for the mode parameter

mode instructions "r" Open in read-only mode, pointing the file pointer to the file header. "r+" Open in read-write mode, pointing the file pointer to the file header. "w" Opens in write mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it. "w+" Opens in read-write mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it. "a" Open in write mode, pointing 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, pointing the file pointer to the end of the file. If the file does not exist, try to create it. "x"

Creates and opens as a write, pointing the file pointer to the file header. If the file already exists, the call to fopen() fails and returns FALSE with an error message of level E_WARNING. If the file does not exist, try to create it.

This and give the underlying open (2) system call to specify O_EXCL | O_CREAT markers are equivalent.

This option is supported by PHP 4.3.2 and later, and can only be used for local files.

"x+"

Creates and opens as a read/write, pointing the file pointer to the file header. If the file already exists, the call to fopen() fails and returns FALSE, generating an error message at the level E_WARNING. If the file does not exist, try to create it.

This and give the underlying open (2) system call to specify O_EXCL | O_CREAT markers are equivalent.

This option is supported by PHP 4.3.2 and later, and can only be used for local files


Related articles: