I want to check if file do not exists. When file_exists()
function returns false
I can't be sure if the file do not exist or I don't have permission to the file.
How to discern that two possibilities?
I want to check if file do not exists. When file_exists()
function returns false
I can't be sure if the file do not exist or I don't have permission to the file.
How to discern that two possibilities?
Check with
is_readable
and if return false, check withfile_exists
I wrote function which check if file can exists. It return false if there is no such file in filesystem, otherwise it returns true. My function checks (bottom-up) directory structure. One should be fairly sure that
$root
directory exists.That is what mean when I wrote:
No, you must have understood something wrong.
file_exists()
will returnTRUE
if the file exists andFALSE
if not. That has nothing to do with permissions of that file.E.g. a file of which my script does not have permissions to read will make
file_exists
returnTRUE
because it exists.However if I test with
is_readable
on that file, it will returnFALSE
. I don't have permissions to read the file.Example:
Output:
Edit: Naturally, this is bound to the underlying system-libraries that PHP makes use of to obtain the information for file existence and file permissions. If PHP is not allowed to obtain the status about whether a file exists or not, it will tell you that the file does not exists. That's for example the case if you've got a directory which exists, but is not readable to the user:
As you would like to obtain the existence status of
$file
, the underlying permissions don't allow you to obtain it. Therefore the file does not exist for you. That's equally correct and you should be more precise what you actually need to find out. Because for you, the file does not exists. That's how directory permissions work (all examples are run on windows here, but these things are that common, that you have it in every common file-system implementation).I hope that sheds some light into your issue.
Well, you could first try file_exists(). In the event that fails, you could try fopen() with the +a flag. If that fails, you don't have permission.
Maybe
is_readable
is what you want?