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?
You have to use CURL
function does_url_exists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) {
$status = true;
} else {
$status = false;
}
curl_close($ch);
return $status;
}
echo is_url_exists('http://stackoverflow.com') ? 'Yes' : 'No';
echoed Yes
. –
Objectivism 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";
By default get_headers uses a GET request to fetch the headers
- so instead of the overhead of a curl request (not really sure what overhead is being referred to by that) - there's a wasteful GET request which drops the body - instead of using a HEAD request and only receiving the headers. –
Yingyingkow $url
is an invalid url you will have error so you can use below code at first of function: if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
–
Tremayne You have to use CURL
function does_url_exists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) {
$status = true;
} else {
$status = false;
}
curl_close($ch);
return $status;
}
echo is_url_exists('http://stackoverflow.com') ? 'Yes' : 'No';
echoed Yes
. –
Objectivism I've just found this solution:
if(@getimagesize($remoteImageURL)){
//image exists!
}else{
//image does not exist.
}
Source: http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/
getimagesize
? As here: uk1.php.net/getimagesize if so then the docs say it can reference local or remote files. –
Lichen If accessing the filename image is impossible getimagesize() will generate an error of level E_WARNING.
–
Carlcarla Hi according to our test between 2 different servers the results are as follows:
using curl for checking 10 .png files (each about 5 mb) was on average 5.7 secs. using header check for the same thing took average of 7.8 seconds!
So in our test curl was much faster if you have to check larger files!
our curl function is:
function remote_file_exists($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( $httpCode == 200 ){return true;}
return false;
}
here is our header check sample:
function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
You can use the function file_get_contents();
if(file_get_contents('https://example.com/example.txt')) {
//File exists
}
Do a request with curl and see if it returns a 404 status code. Do the request using the HEAD request method so it only returns the headers without a body.
$file = 'https://picsum.photos/200/300';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
$headers = get_headers((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . '/uploads/' . $MAIN['id'] . '.pdf');
$fileExist = (stripos($headers[0], "200 OK") ? true : false);
if ($fileExist) {
?>
<a class="button" href="/uploads/<?= $MAIN['id'] ?>.pdf" download>скачать</a>
<? }
?>
© 2022 - 2024 — McMap. All rights reserved.