Skip to content

Instantly share code, notes, and snippets.

@graham73may
Created June 1, 2018 12:17
Show Gist options
  • Save graham73may/6bdcbf1b3c7fd3f7b91b5c20718a7cee to your computer and use it in GitHub Desktop.
Save graham73may/6bdcbf1b3c7fd3f7b91b5c20718a7cee to your computer and use it in GitHub Desktop.
On site and external file size checker
function get_file_size($url, $internal = true)
{
if ($internal) {
$path = parse_url($url, PHP_URL_PATH);
$server_path = $_SERVER['DOCUMENT_ROOT'] . $path;
if (file_exists($server_path)) {
return size_format(filesize($server_path));
}
return null;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
if ($size > 0) {
return size_format($size);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment