PHP: How to check if a file doesn't exist or if permission is allowed?

I want to check if there is a file. When the function file_exists()

returns false

, I cannot be sure that the file does not exist or that I do not have permission to the file.

How do you recognize two possibilities?

+3


source to share


5 answers


Perhaps is_readable

this is what you want?



+1


source


Well, you can try file_exists () first. In case of failure, you can try fopen () with the + a flag. If that fails, you don't have permission.



+1


source


I want to check if there is a file. When the function file_exists()

returns false

, I cannot be sure that the file does not exist or that I do not have permission to the file.

No, you must have got something wrong. file_exists()

will return TRUE

if the file exists, and false

if not. It has nothing to do with the permissions of this file.

eg. a file which my script does not have read permissions will make a file_exists

return TRUE

because it exists.

However, if I test with is_readable

in this file it will return false

. I do not have permission to read the file.

Example:

$file = 'unreadable';
var_dump(file_exists($file), is_readable($file));

      

Output:

bool(true)
bool(false)

      

Change: . Naturally, this has to do with the underlying system libraries that PHP uses to get information about file existence and file permissions. If PHP cannot get the status of the file existence or not, it will tell you that the file does not exist. This is, for example, the case if you have a directory that exists but is not accessible 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)

      

How you would like to get existential status $file

, basic permissions prevent you from getting it. Therefore, the file does not exist for you. This is equally correct and you have to be more precise, which is what you really need to figure out. Because the file doesn't exist for you. This is how directory permissions work (all examples run on Windows here, but these are common things that you have in every common file system implementation).

I hope this sheds light on your problem.

+1


source


I wrote a function that checks if a file exists. It returns false if there is no such file in the file system, otherwise it returns true. My function checks the directory structure (bottom to top). You need to be pretty sure that the directory exists $root

.

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;
}

      

This is what it means when I wrote:

I want to check if there is a file.

+1


source


Check with is_readable

and if return false check the boxfile_exists

0


source







All Articles