Skip to content

Instantly share code, notes, and snippets.

@rendfall
rendfall / get-remote-filesize.php
Created October 16, 2016 20:51
Get remote filesize
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"