Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists from a url

I need to check if a particular file exists on a remote server. Using is_file() and file_exists() doesn't work. Any ideas how to do this quickly and easily?

like image 443
David Avatar asked Oct 07 '11 08:10

David


People also ask

How do you check if a file exists from a URL in PHP?

The file_exists() function in PHP, is used to check if a file or directory exists on the server. But the file_exists() function will not usable if you want to check the file existence on the remote server. The fopen() function is the easiest solution to check if a file URL exists on a remote server using PHP.

Which function is used to check file existence?

The file_exists() function checks whether a file or directory exists.

How check if file exists folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists.

How can check file in folder in PHP?

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.


1 Answers

You don't need CURL for that... Too much overhead for just wanting to check if a file exists or not...

Use PHP's get_header.

$headers=get_headers($url); 

Then check if $result[0] contains 200 OK (which means the file is there)

A function to check if a URL works could be this:

function UR_exists($url){    $headers=get_headers($url);    return stripos($headers[0],"200 OK")?true:false; }  /* You can test a URL like this (sample) */ if(UR_exists("http://www.amazingjokes.com/"))    echo "This page exists"; else    echo "This page does not exist"; 
like image 195
patrick Avatar answered Sep 23 '22 00:09

patrick