Fetch Remote File


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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, [email protected]$matches[1]);
  25.  
  26. } else { return false; }
  27.  
  28. }
  29.  
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.