How do i find out the size of a external file using php

Discussion in 'PHP' started by koolasia, Jan 2, 2007.

Thread Status:
Not open for further replies.
  1. #1
    what i need is

    when i enter the link of the external file the size is displayed please help

    thanks
     
    koolasia, Jan 2, 2007 IP
  2. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #2
    you could download it to your server first and then get the size, but not every hoster allows url file access.
     
    falcondriver, Jan 2, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    like this, requires curl, if you put your mind to it you could probably do exactly the same thing with socket, but no one is paying me, just proof of concept

    
    <?
    $size = "";
    $type = "";
    function donkey_work($ch, $header)
    {
       	global $size;
       	global $type;
       	if(ereg("Content-Length:", $header))
    	{
    		$temp = split(":", $header);
    		$size = trim( $temp[1] );
    	}
    	if(ereg("Content-Type:", $header))
    	{
    		$temp = split(":", $header);
    		$type = trim( $temp[1] );
    	}
       	return strlen($header);
    }
    
    function krfsize($remote_file)
    {
       global $data;
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $remote_file);
       curl_setopt($ch, CURLOPT_HEADER, 1);
       curl_setopt($ch, CURLOPT_NOBODY, 1);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'donkey_work');
       curl_exec ($ch);
       curl_close ($ch);
       return $data;
    }
    function human_bytes( $type = "MB", $precision = "1" )
    {
    	global $size;
    	switch($type)
    	{
    		case "GB": 
    		return round(( $size / 1073741824 ), $precision); 
    		break;
    		
    		case "MB":
    		return round(( $size / 1048576 ), $precision);
    		break;
    	}
    	
    }
    // Only include the file if it's saved seperately
    // include("krfsize.php");
    // Execute this to fill the data first
    krfsize("http://www.jewlzk.com/test100.zip");
    // Will echo the filesize is a human readable format, takes
    // two parameters human_bytes( [string] MB or GB, [int] precision of rounding )
    // both parameters are optional
    echo human_bytes(  );
    // echo human_bytes( "GB" ); # Gigabytes example, change precision for this one
    // echo $size; # $size contains the filesize in bytes
    // echo $type; # $type contains the filetype
    ?>
    
    PHP:
    I should aso mention that php5 ha a get_headers() function built in that will return the headers as an array, but not everyone has php5, or knows how to use an array, it's pretty simple code above and commented where it needs to be, prolly best to save as a file on it's own and include() it when you need it

    Have fun .....
     
    krakjoe, Jan 2, 2007 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Barti1987, Jan 2, 2007 IP
Thread Status:
Not open for further replies.