PHP: How to check if file not exists or permission is denied?

前端 未结 5 2346
礼貌的吻别
礼貌的吻别 2020-12-21 09:05

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 permiss

5条回答
  •  眼角桃花
    2020-12-21 09:41

    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.

提交回复
热议问题