note 15729 deleted from function.filesize by jimw
| From: | jimw@php.net | Date: | Fri, 14 Dec 2001 20:17:53 +0000 |
| Subject: | note 15729 deleted from function.filesize by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22722@lists.php.net to get a copy of this message | ||
This is a function to return the remote file size of a file on a HTTP 1.1 server. Use it like this:
$filesize = returnRemoteFilesize("http://www.php.net/links.php");
function returnRemoteFilesize($url) {
$firstCut = substr($url, 7, strlen($url));
$firstSlashPOS = strpos($firstCut, "/");
$host = substr($firstCut, 0, $firstSlashPOS);
$filename = substr($firstCut, $firstSlashPOS, strlen($firstCut));
$fp = fsockopen ($host, 80);
fputs ($fp, "GET $filename HTTP/1.1\r\nHost: jboy.dyn.dhs.org\r\n\r\n");
for($i=0; $i < 10; $i++) {
$buffer .= fgets ($fp,2000);
}
if(strstr($buffer, "Content-Type")) {
fputs ($fp, "\r\n\r\n\r\n");
fclose ($fp);
}
$contentLengthPOS = strpos($buffer, "Content-Length");
$buffer = substr($buffer, $contentLengthPOS+16);
$newLinePOS = strpos($buffer, "\n");
$buffer = substr($buffer, 0, $newLinePOS);
$answer = sprintf("%".strpos($buffer,
".").".1f",$buffer/1024);
if($answer > 1024) {
$answer = sprintf("%".strpos($answer,
".").".1fmb",$answer/1024);
} else {
$answer .= "kb";
}
$formatAnswer = removeWhiteSpace($answer);
return $formatAnswer;
}