PHP的is_writeable()函数存在的问题
微wx笑
 2020-04-27【编程语言】
 6
 0关键字:
PHP  
PHP的is_writeable 函数存在的问题,同mkdir 函数一样,目录需要有执行的权限,仅有可写的权限是不行的。另外看到一个【题目】PHP的is_writeable()函数存在Bug,无法准确判断一个目
PHP的is_writeable 函数存在的问题,同mkdir 函数一样,目录需要有执行的权限,仅有可写的权限是不行的。
另外看到一个【题目】PHP的is_writeable()函数存在Bug,无法准确判断一个目录/文件是否可写,请写一个函数来判断目录/文件是否绝对可写。
解决方案如下:
/**
 * Tests for file writability
 *
 * is_writable() returns TRUE on Windows servers when you really can't write to
 * the file, based on the read-only attribute.  is_writable() is also unreliable
 * on Unix servers if safe_mode is on.
 *
 * @access  private
 * @return  void
 */
if ( ! function_exists('is_really_writable'))
{
    function is_really_writable($file)
    {
        // If we're on a Unix server with safe_mode off we call is_writable
        if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
        {
            return is_writable($file);
        }
        // For windows servers and safe_mode "on" installations we'll actually
        // write a file then read it.  Bah...
        if (is_dir($file))
        {
            $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
            if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
            {
                return FALSE;
            }
            fclose($fp);
            @chmod($file, DIR_WRITE_MODE);
            @unlink($file);
            return TRUE;
        }
        elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
        {
            return FALSE;
        }
        fclose($fp);
        return TRUE;
    }
}亲测无效,同样存在问题。
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0  许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lang/2020-04-27/415.html



