LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Get Remote File Size Or URL Size In B, KB, MB, GB Format Using PHP?

How To Get Remote File Size Or URL Size In B, KB, MB, GB Format Using PHP?

How-To-Get-Remote-File-Size-Or-URL-Size-In-B,-KB,-MB,-GB-Format-Using-PHP
Do you want to know about how to get external hosted remote file size or URL size is fully formated as Bytes, KiloBytes, MegaBytes, GigaBytes format using pure PHP without having any external class then you are on the right place to find out the code? Before processing the code, here we want to first confirm the logic about how to get this. To know the file or URL size, first, we have to reach or get access to that URL or file that we can do via two popular ways as cURL() and getHeaders(). Both work in different ways.

Sometimes both return the different results as get_headers will do a GET request by default while you configured cURL to do a HEAD request. Start by making the request identical to what cURL sends by putting a different HTTP stream context using HEAD for the request method.

How To Get Remote File Size Or URL Size In B, KB, MB, GB Format Using PHP?

There are many ways to get the file or URL size but here we are sharing the top 3 different ways to get it. You can try all of the below but these have different properties or features that you what to choose as per your requirement. Also, do the changes as per your need.

Recommended For You:
Drag And Drop Reorder List & DIV Using Pure JavaScript

1.) Send A cURL Head Request And Extract ["Content-Length"] Value From The Header:

Here’s the best way (that I’ve found) to get the size of a remote file. Note that HEAD requests don’t get the actual body of the request, they just retrieve the headers. So making a HEAD request to a resource that is 100MB will take the same amount of time as a HEAD request to a resource that is 1KB.

function curlRemoteFilesize( $url, $formatSize = true )
{
// Assume Failure
$result = 0;

$curl = curl_init( $url );

// Issue a HEAD request and follow any redirects.
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$data = curl_exec( $curl );

curl_close( $curl );

if( $data ) {
$content_length = 0;
$status = 0;

if( preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches) ) {
$status = (int)$matches[1];
}

if( preg_match("/Content-Length: (\d+)/", $data, $matches) ) {
$content_length = (int)$matches[1];
}

// http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if( $status == 200 || ($status > 300 && $status <= 308) ) {
$result = $content_length;
}
}

if (!$formatSize) {
return $result;
// return size in bytes
}

$size = $result;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GB'; break;
}
return $size;
}

2.) Send A get_headers() GET Request And Extract ["Content-Length"] Value From The Header:

Using a HEAD request and checking for Content-Length is the standard way to do it, but you can’t rely on it in general, since the server might not support it. The Content-Length header is optional, and further, the server might not even implement the HEAD method. If you know which server you’re probing, then you can test if it works, but as a general solution it isn’t bulletproof.

function getRemoteFilesize($file_url, $formatSize = true)
{
$head = array_change_key_case(get_headers($file_url, 1));
// content-length of download (in bytes), read from Content-Length: field
$clen = isset($head['content-length']) ? $head['content-length'] : 0;
// cannot retrieve file size, return “-1”
if (!$clen) {
return 0;
}
if (!$formatSize) {
return $clen;
// return size in bytes
}
$size = $clen;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GB'; break;
}
return $size;
}

3.) Alternate Code If ["Content-Length"] Is Not Present In Header:

Unfortunately, there are many HTTP servers out there that don’t function accurately with regards to the Content-Length header. Since the HEAD the response has no actual content they return a different Content-Length than a GET would do.

Recommended For You:
Stylish CSS Shadow Boxes Widgets Using Pure CSS3

If Content-Length the header is not present, which means that file size is unknown. In your case, you have Content-Disposition header, which means that a downloadable file is an attachment to the server response (same as an email attachment). So here if you have no Content-Length header you can’t get the size.

You won’t know the size of the content in advance without a Content-Length header.

If you can’t control the server then you’re going to have to handle it. One option would be to stream the data into a temporary file on disk, and once you have that, you can determine the size, and pass it on to the next step in whatever you’re doing.

You could stream it into a memory buffer but since you don’t how big it will be, there’s the risk that you’ll end up running out of memory.

So the only solution is to download and read the content and find out the strlen() that is equal to bytes and then follow the next processes.

function curlRemoteFileContentsize( $url, $formatSize = true )
{
$ch = curl_init();
$timeout = 5; //5 is seconds
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
// this function is called by curl for each header received
$tempCURLVar = curl_exec($ch);
$webPageContent = $tempCURLVar;
curl_close($ch);
$result = mb_strlen($webPageContent);

if (!$formatSize) {
return $result;
// return size in bytes
}

$size = $result;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GB'; break;
}
return $size;
}

4.) Alternate Code If Do Not Want To Count Content Length/String Length:

function curlRemoteFileContentsize( $url, $formatSize = true )
{
$ch = curl_init();
$timeout = 5; //5 is seconds
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
// this function is called by curl for each header received
$webPageContent = curl_exec($ch);
$result = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
curl_close($ch);

if (!$formatSize) {
return $result;
// return size in bytes
}

$size = $result;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GB'; break;
}
return $size;
}

Final Words:

Be aware that the is placed well in your document. Rest all is in your hand if you want to customize it or play with it. That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allowed to us. Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
How To Create An HTML Working Contact Form For Your Blog & Website

You Like It, Please Share This Recipe With Your Friends Using...

2 Responses to “How To Get Remote File Size Or URL Size In B, KB, MB, GB Format Using PHP?”

  1. Thanks For Sharing this informative post. I also want to try these on my website.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks for reading this article.

Leave a Reply to Digital Marketing Agency In Delhi Cancel reply

Your email address will not be published. Required fields are marked *