1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| # samuel dot l at mushicrew dot com
#03-Feb-2006 05:18
// If you can't, for any reason, use the CURL extension on your server, you // can use this function to request filesize on HTTP servers.
<?php
function http_filesize($sURL, $nTimeout = 30) {
// Parsing URL
$aURL = parse_url($sURL);
// Checking HTTP request
if($aURL['scheme'] != 'http')
return false;
// HTTP Host
$sHost = $aURL['host'];
// Remote file path
$sFilepath = (isset($aURL['path']) ? $aURL['path'] : '/') . (isset($aURL['query']) ? '?' . $aURL['query'] : '');
// Remote port
$nPort = isset($aURL['port']) ? $aURL['port'] : 80;
// Socket opening
$fpRemote = fsockopen($sHost, $nPort, $errno, $errstr, $nTimeout);
// Make sure the socket is open
if(!$fpRemote)
return false;
else {
// Request headers
$sHeaders = "HEAD " . $sFilepath . " HTTP/1.1\r\n";
$sHeaders .= "Host: ". $sHost . "\r\n";
$sHeaders .= "Connection: Close\r\n\r\n";
// Sending headers
fwrite($fpRemote, $sHeaders);
// Getting back the content
$sRemoteHeaders = '';
while(!feof($fpRemote))
$sRemoteHeaders .= fgets($fpRemote, 128);
// Closing the socket
fclose($fpRemote);
// Parsing the headers
preg_match('/Content-Length:\s([0-9].+?)\s/', $sRemoteHeaders, $aMatches);
if(isset($aMatches[1]))
return (int)$aMatches[1];
else
return false;
}
}
?>
// Not that this function will return FALSE if the socket can't connect, or if the server doesn't provide the Content-Length header. |
Partager