PHP判断文件是否存在
这个代码可以判断远程文件或者本地代码,其实主要作用就是判断远程文件,毕竟判断本地文件直接一个file_exists就行了。直接上代码:
function my_file_exists($file) {
if(preg_match('/^http:\/\//',$file)){ // 如果是判断https则这里改成https
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);
}
页:
[1]