Quick Tip: Download a Remote File Using PHP

Posted August 20, 2009 @ 4:16pm by Phil

Here is a function I wrote for one of my projects that allows you to download a file from a remote server and save it to your server:

<?php
  function download_file ($src, $dest)
  {
    $handle = fopen($src, "rb");
    $contents = "";
    while (!feof($handle))
    {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
    
    $handle = fopen($dest, "wb");
    fwrite($handle, $contents);
    fclose($handle);
  }
?>

Download Source: download_file.php

Tags: PHP/MySQL, Quick Tip, Webmasters Tips