I'm having a very weird issue with file_exists(). I'm using this function to check if 2 different files in the same folders do exist. I've double-checked, they BOTH do exist.
echo $relative . $url['path'] . '/' . $path['filename'] . '.jpg'; Result: ../../images/example/001-001.jpg echo $relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension']; Result: ../../images/example/001-001.PNG
Now let's use file_exists() on these:
var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.jpg')); Result: bool(false) var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension'])); Result: bool(true)
I don't get it - both of these files do exist. I'm running Windows, so it's not related to a case-sensitive issue. Safe Mode is off.
What might be worth mentioning though is that the .png one is uploaded by a user via FTP, while the .jpg one is created using a script. But as far as I know, that shouldn't make a difference.
Any tips?
Thanks
Files. exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.
The file_exists() function checks whether a file or directory exists.
PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).
The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False. Parameters Used: The is_dir() function in PHP accepts only one parameter.
Results of the file_exists()
are cached, so try using clearstatcache()
. If that not helped, recheck names - they might be similar, but not same.
file_exists()
just doesn't work with HTTP addresses.
It only supports filesystem paths (and FTP, if you're using PHP5.)
Please note:
Works :
if (file_exists($_SERVER['DOCUMENT_ROOT']."/folder/test.txt") echo "file exists";
Does not work:
if (file_exists("www.mysite.com/folder/test.txt") echo "file exists";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With