Posted By

Abe on 10/01/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

luman
bobbym245


Get Remote File Size


 / Published in: PHP
 

URL: http://www.weberdev.com/get_example-4610.html

  1. function get_remote_file_size($url) {
  2. $parsed = parse_url($url);
  3. $host = $parsed["host"];
  4. $fp = @fsockopen($host, 80, $errno, $errstr, 20);
  5. if (!$fp) return false;
  6. else {
  7. @fputs($fp, "HEAD $url HTTP/1.1
  8. ");
  9. @fputs($fp, "HOST: $host
  10. ");
  11. @fputs($fp, "Connection: close
  12.  
  13. ");
  14. $headers = "";
  15. while(!@feof($fp))$headers .= @fgets ($fp, 128);
  16. }
  17. @fclose ($fp);
  18. $return = false;
  19. $arr_headers = explode("\n", $headers);
  20. foreach ($arr_headers as $header) {
  21. $s = "Content-Length: ";
  22. if (substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
  23. $return = trim(substr($header, strlen($s)));
  24. break;
  25. }
  26. }
  27. if ($return) {
  28. $size = round($return / 1024, 2);
  29. $sz = "KB"; // Size In KB
  30. if ($size > 1024) {
  31. $size = round($size / 1024, 2);
  32. $sz = "MB"; // Size in MB
  33. }
  34. $return = "$size $sz";
  35. }
  36. return $return;
  37. }

Report this snippet  

You need to login to post a comment.