Home  >  Article  >  Backend Development  >  浅析is_writable的php实现_php技巧

浅析is_writable的php实现_php技巧

WBOY
WBOYOriginal
2016-05-17 09:00:44893browse

以下函数可用于替换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;
        }
    }
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn