The following describes how to obtain the Remote File Size in PHP in detail. For more information, see
The following describes how to obtain the Remote File Size in PHP in detail. For more information, see
1. Use file_get_contents ()
The Code is as follows:
$ File = file_get_contents ($ url );
Echo strlen ($ file );
?>
2. Use get_headers ()
The Code is as follows:
$ Header_array = get_headers ($ url, true );
$ Size = $ header_array ['content-length'];
Echo $ size;
?>
PS:
You need to open allow_url_fopen!
If the meeting is not held
Warning: get_headers () [function. get-headers]: URL file-access is disabled in the server configuration
3. Use fsockopen ()
The Code is as follows:
Function get_file_size ($ url ){
$ Url = parse_url ($ url );
If (empty ($ url ['host']) {
Return false;
}
$ Url ['Port'] = empty ($ url ['post'])? 80: $ url ['post'];
$ Url ['path'] = empty ($ url ['path'])? '/': $ Url ['path'];
$ Fp = fsockopen ($ url ['host'], $ url ['Port'], $ error );
If ($ fp ){
Fputs ($ fp, "GET". $ url ['path']. "HTTP/1.1/r \ n ");
Fputs ($ fp, "Host:". $ url ['host']. "\ r \ n ");
While (! Feof ($ fp )){
$ Str = fgets ($ fp );
If (trim ($ str) = ''){
Break;
} Elseif (preg_match ('/Content-Length :(. *)/si', $ str, $ arr )){
Return trim ($ arr [1]);
}
}
Fclose ($ fp );
Return false;
} Else {
Return false;
}
}
?>
, Record-free space, Hong Kong virtual host, Hong Kong server