php 判断文件是否可写 ci is_really_writable()

PHP 中原生的 is_writable() 函数在 windows 系统中不能准确判断文件是否可写, 如果文件只是可读 is_writable() 也会返回 true, 无法达到真正的判断目的. 而如果是在 Unix 内核的系统中, 在配置文件 safe_mode 参数被设置为 on 时 is_writable() 函数也不奏效.为了避免这样的问题可以参考CodeIgniter中的is_really_writable函数(位于:system/core/common.php)

function is_really_writable($file)
     {
    // 在 Unix 内核系统中关闭了 safe_mode, 可以直接使用 is_writable()
    if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
    {
        return is_writable($file);
    }

    // 在 Windows 系统中打开了 safe_mode的情况
    if (is_dir($file))
    {
        $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));

        if (($fp = @fopen($file, 'ab')) === FALSE)
        {
            return FALSE;
        }

        fclose($fp);
        @chmod($file, 0777);
        @unlink($file);
        return TRUE;
    }
    elseif (($fp = @fopen($file, 'ab')) === FALSE)
    {
        return FALSE;
    }

    fclose($fp);
    return TRUE;
}

 

posted @ 2013-03-12 17:57  欧麦噶地  阅读(832)  评论(0编辑  收藏  举报