这个代码可以判断远程文件或者本地代码,但其实主要作用就是判断远程文件,判断本地文件建议直接是要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); }
以上就是php判断文件是否存在实例教程,更多php教程关注菜鸟建站。
发表评论