CODER

RECORDING, ENJOY LIFE

file_exists 函数重构判断本地、远程文件是否存在
2017年8月9日 11:01
<?php
    function my_file_exists($file){
        if(preg_match('/^http:\/\//', $file)){
            //远程文件
            if(ini_get('allow_url_fopen')){
                if(@fopen($file, 'r')) return true;
            }else{
                $parseurl = parse_url($file);
                $host = $parseurl['host'];
                $path = $parseurl['path'];
                $fp = fsockopen($host, 80, $errno, $errstr, 10);
                if(!$fp) return false;
                fputs($fp, "GET {$path} HTTP/1.1 \r\nhost:{$host}\r\n\r\n");
                if(preg_match('/HTTP\/1.1 200/', fgets($fp,1024))) return true;
            }
            return false;
        }
        return file_exists($file);
    }
?>
LinuxNginxApachePHPMysqlThinkPHPCSS3JQueryVB其他