Posted By

neogeek on 06/29/06


Tagged

fetch remote file socket


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

luman
24baud
jacksont123


Fetch Remote File


 / Published in: PHP
 

This simple function allows you to fetch files from other web sites in PHP4 without needing to install any extensions for PHP.

  1. if (!function_exists('fetch_remote_file')) {
  2.  
  3. function fetch_remote_file($file) {
  4.  
  5. $path = parse_url($file);
  6.  
  7. $fs = @fsockopen($path['host'], 80);
  8.  
  9. if ($fs) {
  10.  
  11. $header = 'GET ' . $file . ' HTTP/1.0' . lnbr;
  12. $header .= 'Host: ' . $path['host'] . str_repeat(lnbr, 2);
  13.  
  14. fwrite($fs, $header);
  15.  
  16. $buffer = '';
  17.  
  18. while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; }
  19.  
  20. $matches = Array();
  21.  
  22. preg_match('/Content-Length: ([0-9]+)/', $buffer, $matches);
  23.  
  24. return substr($buffer, -@$matches[1]);
  25.  
  26. } else { return false; }
  27.  
  28. }
  29.  
  30. }

Report this snippet  

You need to login to post a comment.