首页 > 编程 > PHP > 正文

浅析is_writable的php实现

2019-11-22 00:26:27
字体:
来源:转载
供稿:网友
以下函数可用于替换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;
        }
    }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表