Welcome 微信登录

首页 / 网页编程 / PHP / 浅析is_writable的php实现

以下函数可用于替换php内置的is_writable函数
复制代码 代码如下:
//可用于替换php内置的is_writable函数
function isWritable($filename){
    if(preg_match("//$/",$filename)){
        $tmp_file=sprintf("%s%s.tmp",$filename,uniqid(mt_rand()));
        return isWritable($tmp_file);
    }
    if(file_exists($filename)){
        //文件已经存在的话,使用读写方式打开
        $fp=@fopen($filename,"r+");
        if($fp){
            fclose($fp);
            return true;
        }
        else{
            return false;
        }
    }
    else{
        $fp=@fopen($filename,"w");
        if($fp){
            fclose($fp);
            unlink($filename);
            return true;
        }
        else{
            return false;
        }
    }
}