رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
بدست آوردن اندازه فایلها با واحد B و KB و MB و... بطور خودکار
#1
<?php
    function FormatSize($url, $remote = false, $precision = 2) {
        $bytes = 0;
        if(!$remote) {
            if(file_exists($url)) {
                $bytes = filesize($url);
            }
        }
        else {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_NOBODY, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Not necessary unless the file redirects
            $data = curl_exec($ch);
            curl_close($ch);
            if ($data === false) {
              return -1;
            }
            if (preg_match('#Content-Length: (d+)#i', $data, $matches)) {
              $bytes = trim($matches[1]);
            }
        }
        settype($bytes, 'double');
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $len = count($units);
        for ($i = 0; $bytes >= 1024 && $i < $len; $i++) {
            $bytes /= 1024;
        }
        return round($bytes, $precision) . $units[$i];
    }
    /* Usage Samples:
    echo '<p>' . FormatSize('images/logo.png') . '</p>' . PHP_EOL;
    echo '<p>' . FormatSize('http://www.google.com/images/srpr/logo4w.png', true) . '</p>' . PHP_EOL;
    */
?>

این اسکریپت حتی سایز فایل سایتهای دیگه و حتی لینک فایلهای RapidShare و... رو هم میتونه استخراج کنه.
پاسخ
تشکر شده توسط: php




کاربران در حال بازدید این موضوع: 1 مهمان