-10

Hey i made a program which is showing the size of the remote url but i don't wanted the output to come like 4.3224256231 MB , i want that only first 3 digits should come as a output like 4.32 MB. Here is my PHP code:-

<?php

function remote_file_size($url){
    $head = "";
    $url_p = parse_url($url);

    $host = $url_p["host"];
    if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){

        $ip=gethostbyname($host);
        if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){

            return -1;
        }
    }
    if(isset($url_p["port"]))
    $port = intval($url_p["port"]);
    else
    $port    =    80;

    if(!$port) $port=80;
    $path = $url_p["path"];

    $fp = fsockopen($host, $port, $errno, $errstr, 20);
    if(!$fp) {
        return false;
        } else {
        fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n");
        fputs($fp, "HOST: " . $host . "\r\n");
        fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        $headers = "";
        while (!feof($fp)) {
            $headers .= fgets ($fp, 128);
            }
        }
    fclose ($fp);

    $return = -2;
    $arr_headers = explode("\n", $headers);
    foreach($arr_headers as $header) {

        $s1 = "HTTP/1.1";
        $s2 = "Content-Length: ";
        $s3 = "Location: ";

        if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
        if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2));
        if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));  
    }

    if(intval($size) > 0) {
        $return=intval($size);
    } else {
        $return=$status;
    }

    if (intval($status)==302 && strlen($newurl) > 0) {

        $return = remote_file_size($newurl);
    }
    return $return;
}
$file= remote_file_size("http://funchio.com/mp3/download.php?hash=7RLenrUE&name=nagada%20sang%20dhol%20baje-ramleela");
$p=$file/1048;
$m=$p/1048;
echo $m. " MB";
substr_replace($m, "", -9)


?>

Please help me to do this.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    This is really something you should be able to find on your own... Just typing "PHP round number" into google yields **a ton** of results and examples. There's also quite some examples in the PHP docs on round/floor/ceil/sprintf. – ccKep May 05 '14 at 06:55
  • use PHP [Round](http://www.php.net/manual/en/function.round.php) function – Lepanto May 05 '14 at 06:56
  • 1
    Also: I don't think dividing by `1048` is going to yield you correct results... There are `2^10 = 1024` Byte in a KByte, same goes for KByte => MByte and so on. – ccKep May 05 '14 at 06:56
  • user `round()` function – krishna May 05 '14 at 06:59

3 Answers3

0

User number_format(); First parameter is the number being formatted. and the second parameter is number of decimal points.

echo number_format($m,3). " MB";
Rahul Kaushik
  • 1,454
  • 8
  • 16
0

try..

echo round($m, 2);

echo round(4.3224256231, 2); 

output: 4.32

you might also add additional logic that specifies GB

krishna
  • 4,069
  • 2
  • 29
  • 56
0

Use this simple function for getting remote filesize. Your function is too redundant.

function remote_filesize($url) {
    static $regex = '/^Content-Length: *+\K\d++$/im';
    if (!$fp = @fopen($url, 'rb')) {
        return false;
    }
    if (
        isset($http_response_header) &&
        preg_match($regex, implode("\n", $http_response_header), $matches)
    ) {
        return (int)$matches[0];
    }
    return strlen(stream_get_contents($fp));
}
mpyw
  • 5,526
  • 4
  • 30
  • 36