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?
Maybe is_readable
is what you want?
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.
I want to check if file do not exists. When
file_exists()
function returnsfalse
I can't be sure if the file do not exist or I don't have permission to the file.
No, you must have understood something wrong. file_exists()
will return TRUE
if the file exists and FALSE
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
return TRUE
because it exists.
However if I test with is_readable
on that file, it will return FALSE
. I don't have permissions to read the file.
Example:
$file = 'unreadable';
var_dump(file_exists($file), is_readable($file));
Output:
bool(true)
bool(false)
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:
$dir = 'unreadable';
$file = $dir.'/unreadable.ext';
var_dump(file_exists($dir), is_readable($dir));
# bool(true)
# bool(false)
var_dump(file_exists($file), is_readable($file));
# bool(false)
# bool(false)
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.
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.
private function fileCanExists($root, $path) {
$root .= '/';
if (file_exists($root . $path))
return true;
while ($path != '.') {
$path = dirname($path);
if (file_exists($root . $path)) {
if (is_readable($root . $path))
return false;
else
return true;
}
}
return false;
}
That is what mean when I wrote:
I want to check if file do not exists.
Check with is_readable
and if return false, check with file_exists