File_Exists() Returns False Even If File Exist (Remote Url)

file_exists() returns false even if file exist (remote URL)

$filename= 'https://www.google.pl/logos/2012/haring-12-hp.png';
$file_headers = @get_headers($filename);

if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
echo "The file $filename does not exist";
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
echo "The file $filename does not exist, and I got redirected to a custom 404 page..";
} else {
echo "The file $filename exists";
}

file_exists() returns false, but the file DOES exist

Results of the file_exists() are cached, so try using clearstatcache(). If that not helped, recheck names - they might be similar, but not same.

PHP and Apache file_exists not working from URL

The web server user account must have permission to read the file - this is a different user account than the one you use on console.

sudo chmod +rx /mnt/disk1/a.jpg

Apache also needs execute access on all folders to root.

sudo chmod +x /mnt/disk1
sudo chmod +x /mnt

file_exists() returns false, even for paths that do exist

That should be:

<?php 
$photolocation = '../wp-content/gallery/playerphotos/XXX Smith.jpg';

if (file_exists($photolocation))
{
echo "File exists";
}
else
{
echo "File does not exist";
}
?>

file_exists returns wrong value

According to the PHP manual, file_exists checks whether a file exists given a path to a file or directory.

This means that you cannot provide a URL to check, but rather an absolute or relative file path on the local filesystem. If you wish to check the file at a given URL, there is something in the comments of that page. Here is the relevant code:

$file = 'https://strutmymutt.com/file/pic/pages/13822542191645543571_400.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] === 'HTTP/1.1 404 Not Found') {
echo 'no';
} else {
echo 'yes';
}

How to check if a file exists from a url

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;
}

How to check if a file exist on an external server

  1. Make sure error reporting is on.

  2. Use if($handle)

  3. Check allow_url_fopen is true.

  4. If none of this works, use this method on the file_exists page.



Related Topics



Leave a reply



Submit