How can I solve the fsockopen function error ;? // Obtain the file size function & nbsp; remote_filesize ($ url_file) {& nbsp; if & nbsp ;(! Remote_file_exists ($ url_file) & find out how to solve the fsockopen function, error
// Get the file size
Function remote_filesize ($ url_file ){
If (! Remote_file_exists ($ url_file) return false;
$ HeadInf = get_headers ($ url_file, 1 );
Return $ headInf ['content-length'];
}
// Determine whether a file exists
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 ";
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 ");
?>
This is an example of remote file size I found online.
If the domain name is normal, the file size will be returned if the file exists,
If the domain name is normal and the file does not exist, false is returned.
Now the problem occurs. if the domain name is abnormal, it is like the above address. the domain name cannot be resolved, that is, it cannot be accessed. At this time, an error message is output.
Php_network_getaddresses: getaddrinfo failed: Unknown host. In D: \ PHPnow-1.5.6 \ htdocs \ webadmin \ inc \ filesize. php on line 15
How can I remove this error message, and solve it in the code without modifying the web server configuration.
Because I want to use ajax to retrieve the file size information. Therefore, no error is reported. When an error is reported, the return value obtained by javascript is the error message, which causes the foreground to fail to run.
Yes? give me some advice. Share:
------ Solution --------------------
$ Fp = @ fsockopen ($ host, $ port, $ err_no, $ err_str, 30 );
Shield error messages (this function was born before exception handling)