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
Make sure error reporting is on.
Use
if($handle)
Check allow_url_fopen is true.
If none of this works, use this method on the file_exists page.
Related Topics
PHP String Interpolation Syntax
Using Simplexml to Load Remote Url
Disable Browser Cache in PHP or JavaScript in a Flash Application
PHP Run Once and Insert Twice in MySQL Database
Jquery: Running Ajax Locally Without a Webserver
Why Use Sprintf Function in PHP
Get Image Height and Width as Integer Values
PHP Form Token Usage and Handling
Apply a Coupon Programmatically in Woocommerce
How to Use Array_Unique on an Array of Arrays
How to Run a .Sh File from PHP
PHP - Add Link to a Url in a String
How to Reverse a Unicode String