<?
//获取文件大小
function remote_filesize($url_file){
    if (!remote_file_exists($url_file)) return false;
    $headInf = get_headers($url_file,1);
    return $headInf['Content-Length'];
}
//判断文件是否存在
function remote_file_exists($url_file){ 
    $url_file = trim($url_file);
    if (empty($url_file)) return false;
    $url_arr = parse_url($url_file);
    if (!is_array($url_arr) || empty($url_arr)) return false;
    $host = $url_arr['host'];
    $path = $url_arr['path'] ."?".@$url_arr['query'];
    $port = isset($url_arr['port']) ?$url_arr['port'] : "80";
    $fp = fsockopen($host, $port, $err_no, $err_str,30);
    if (!$fp) return false;
    $request_str = "GET ".$path." HTTP/1.1\r\n";
    $request_str .= "Host:".$host."\r\n";
    $request_str .= "Connection:Close\r\n\r\n";
    fwrite($fp,$request_str);
    //fread replace fgets
    $first_header = fread($fp, 128);
    fclose($fp);
    if (trim($first_header) == "") return false;
    //check $url_file "Content-Location"
    if (!preg_match("/200/", $first_header) || preg_match("/Location:/", $first_header)) return false;
    return true;
}
echo remote_filesize("http://wlm212.bjphp1.qq1.cc/down/ee.exe");
?>
这个是我在网上找的,取远程文件大小的例子。
如果域名正常,文件存在就返回文件大小,
如果域名正常,文件不存在,就返回false。现在问题出现了,如果域名不正常,就像上面这个地址,域名解析不了,也就是无法访问。这个时候会输出错误信息。php_network_getaddresses: getaddrinfo failed: 不知道这样的主机。 in D:\PHPnow-1.5.6\htdocs\webadmin\inc\filesize.php on line 15怎样能把这个错误信息去掉呢,在不修改web服务器配置的情况下,只在代码里面解决。
因为我要用ajax,取文件大小信息。所以不能报错,一报错,javascript获取的返回值,就是错误信息了,这个就导致前台无法运行了。有会的吗,指点一下小弟。