تشخیص حجم فایل یک لینک
مثلا حجم یک فایل زیپ یک لینک دانلود رو که از سایت دیگه است رو مشخص میکنه
کد PHP:
<?php
$remoteFile = 'http://download.thinkbroadband.com/5MB.zip';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (float)$matches[1];
}
//echo 'HTTP Status: ' . $status . "\n";
//echo 'Content-Length: ' . $contentLength;
function format_bytes($contentLength) {
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $contentLength >= 1024 && $i < 4; $i++) $contentLength /= 1024;
return round($contentLength, 2).$units[$i];
}
echo format_bytes($contentLength);
?>