Skip to content

Instantly share code, notes, and snippets.

@eyecatchup
Last active August 21, 2020 02:35
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save eyecatchup/f26300ffd7e50a92bc4d to your computer and use it in GitHub Desktop.
Save eyecatchup/f26300ffd7e50a92bc4d to your computer and use it in GitHub Desktop.
<?php
/**
* Get the file size of any remote resource (using curl),
* either in bytes or - default - as human-readable formatted string.
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @license MIT <http://eyecatchup.mit-license.org/>
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
*
* @param string $url Takes the remote object's URL.
* @param boolean $formatSize Whether to return size in bytes or formatted.
* @param boolean $useHead Whether to use HEAD requests. If false, uses GET.
* @return string Returns human-readable formatted size
* or size in bytes (default: formatted).
*
* <code>
* //example
* echo getRemoteFilesize('https://github.com/eyecatchup/SEOstats/archive/master.zip');
* </code>
*/
function getRemoteFilesize($url, $formatSize = true, $useHead = true)
{
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
));
if (false !== $useHead) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_exec($ch);
// content-length of download (in bytes), read from Content-Length: field
$clen = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
// cannot retrieve file size, return "-1"
if (!$clen) {
return -1;
}
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) .' KiB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB'; break;
}
return $size; // return formatted size
}
$url = 'https://dl.google.com/dl/android/aosp/hammerhead-mra58k-factory-52364034.tgz';
echo getRemoteFilesize($url); // echoes "565.66 MiB"
<?php
/**
* Get the file size of any remote resource (using get_headers()),
* either in bytes or - default - as human-readable formatted string.
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @license MIT <http://eyecatchup.mit-license.org/>
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
*
* @param string $url Takes the remote object's URL.
* @param boolean $formatSize Whether to return size in bytes or formatted.
* @param boolean $useHead Whether to use HEAD requests. If false, uses GET.
* @return string Returns human-readable formatted size
* or size in bytes (default: formatted).
*
* <code>
* //example
* echo getRemoteFilesize('https://github.com/eyecatchup/SEOstats/archive/master.zip');
* </code>
*/
function getRemoteFilesize($url, $formatSize = true, $useHead = true)
{
if (false !== $useHead) {
stream_context_set_default(array('http' => array('method' => 'HEAD')));
}
$head = array_change_key_case(get_headers($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 -1;
}
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) .' KiB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB'; break;
}
return $size; // return formatted size
}
$url = 'https://dl.google.com/dl/android/aosp/hammerhead-mra58k-factory-52364034.tgz';
echo getRemoteFilesize($url); // echoes "565.66 MiB"
@kicktv
Copy link

kicktv commented May 18, 2020

I was looking for him ...
thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment