Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_exists() returns false, but the file DOES exist

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

like image 570
Bv202 Avatar asked Aug 03 '11 16:08

Bv202


People also ask

Which of the following returns true if the file at the specified path exists or false otherwise?

Files. exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.

How do you check is file exist in PHP?

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

How do you create a file if it doesn't exist in PHP?

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).

Is PHP a directory or file?

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.


2 Answers

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

like image 145
Timur Avatar answered Oct 20 '22 14:10

Timur


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"; 
like image 24
Mahdi Loghmani Avatar answered Oct 20 '22 12:10

Mahdi Loghmani