4

I want to get the size of a remote file. And it can be done via this code:

$headers = get_headers("http://addressoffile", 1);
$filesize= $headers["Content-Length"];

But i do not know the file address directly. But i have a address which redirects to the original file.

For example: I have address http://somedomain.com/files/34 When i put this address into url bar of browser or use function file_get_contents('myfile.pdf',"http://somedomain.com/files/34"); , it starts downloading the original file.

And if i use above functions for calculating size of file then using the address http://somedomain.com/files/34 it return size 0.

Is there any way to get the address where the http://somedomain.com/files/34 redirects .

Or any other solution for calculating the size of the redirected file(original file).

Vinit Chouhan
  • 686
  • 1
  • 10
  • 23

3 Answers3

8

If you want to get a remote file's size you need to think some other way. In this post I am going to show you how we can get a remote file's size from it's header information with out downloading file. I will show you two example. One, using get_headers function and another using cURL. Using get_headers is very simple approach and works for every body. Using cURL is more advance and powerful. You can use any one of these. But I recommend to use cURL approach. Here we go..

get_headers Approach:

/**
* Get Remote File Size
*
* @param sting $url as remote file URL
* @return int as file size in byte
*/
function remote_file_size($url){
# Get all header information
$data = get_headers($url, true);
# Look up validity
if (isset($data['Content-Length']))
    # Return file size
    return (int) $data['Content-Length'];
}

usage

echo remote_file_size('http://www.google.com/images/srpr/logo4w.png');

cURL Approach

/**
* Remote File Size Using cURL
* @param srting $url
* @return int || void
*/
function remotefileSize($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_exec($ch);
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
if ($filesize) return $filesize;
}

usage

echo remotefileSize('http://www.google.com/images/srpr/logo4w.png');
Prasanna
  • 99
  • 3
  • Thanks for answer, but i already have tried these methods you described but my problem was with getting size of redirected url. @gries solved this problem perfectly – Vinit Chouhan Jan 07 '15 at 11:02
4

If the site redirects via. the Location header you can use:

// get the redirect url
$headers = get_headers("http://somedomain.com/files/34", 1);
$redirectUrl = $headers['Location'];

// get the filesize
$headers = get_headers($redirectUrl, 1);
$filesize = $headers["Content-Length"];

Please note that this code should not be used in production as there are no checks for existing array keys or error handling.

gries
  • 1,135
  • 6
  • 29
0

cURL Approach is good because in some servers get_headers is disable. But if your url have http,https and ... , you need this:

<?php

function remotefileSize($url)
{
    //return byte
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_exec($ch);
    $filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);
    if ($filesize) return $filesize;
}

$url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
echo remotefileSize($url);

?>
Milad Ghiravani
  • 1,625
  • 23
  • 43