function get_remote_file_size($url, $readable = true){ $parsed = parse_url($url); $host = $parsed["host"]; $fp = @fsockopen($host, 80, $errno, $errstr, 20); if(!$fp) return false; else { @fputs($fp, "HEAD $url HTTP/1.1\r\n"); @fputs($fp, "HOST: $host\r\n"); @fputs($fp, "Connection: close\r\n\r\n"); $headers = ""; while(!@feof($fp))$headers .= @fgets ($fp, 128); } @fclose ($fp); $return = false; $arr_headers = explode("\n", $headers); foreach($arr_headers as $header) { // follow redirect $s = 'Location: '; if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) { $url = trim(substr($header, strlen($s))); return get_remote_file_size($url, $readable); } // parse for content length $s = "Content-Length: "; if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) { $return = trim(substr($header, strlen($s))); break; } } if($return && $readable) { $size = round($return / 1024, 2); $sz = "KB"; // Size In KB if ($size > 1024) { $size = round($size / 1024, 2); $sz = "MB"; // Size in MB } $return = "$size $sz"; } return $return; }
Never been to CodeSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Get remote file size, following redirects (PHP) (See related posts)
You need to create an account or log in to post comments to this site.
Related Posts
» PHP HTTP Connection class in php http
» Getting HTTP Auth to play ni... in php fastcgi http auth
» Find largest files on Mac OS X in find awk shell osx mac bash unix file ls size mdfind largest
» Suexec'ed PHP-FastCGI on Apa... in apache php fastcgi suexec
» My lighttpd php-fastcgi conf... in php fastcgi lighttpd
» SSH tunneling for MySQL in mysql ssh tunneling remote
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails