Home >Backend Development >PHP Tutorial >How to Check File Existence on Remote Servers Without Using File Functions?

How to Check File Existence on Remote Servers Without Using File Functions?

DDD
DDDOriginal
2024-10-18 18:48:29821browse

How to Check File Existence on Remote Servers Without Using File Functions?

Determining File Existence on Remote Servers

Question: How can I ascertain the existence of a file on a remote server without utilizing file-related functions?

Answer: PHP's get_header function offers an efficient method to check file availability on remote servers without the need for complex mechanisms.

<code class="php">$headers=get_headers($url);</code>

By inspecting the response status code returned in $headers[0], you can determine if the file exists. A code of "200 OK" indicates that the file is present.

<code class="php">function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}</code>

This function returns a Boolean value indicating the existence or absence of the file at the specified URL. You can leverage it to test URL functionality, as exemplified below:

<code class="php">if(UR_exists("http://www.amazingjokes.com/"))
   echo "This page exists";
else
   echo "This page does not exist";</code>

The above is the detailed content of How to Check File Existence on Remote Servers Without Using File Functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn