Download Remote File with PHP to Server and finally output to browser

Shown below is the code to download a remote file with PHP and save to the server and finally output to browser using cURL and readfile



  1. Since the file is downloaded to the server first before outputting to the browser, it consumes time, bandwidth & server space.
  2. When downloading large files, the script may exhaust the memory limit and is prone to timeout.

4 Responses

  1. November 10, 2013 / 7:50 PM

    Well this is a good think, its not downloading the whole parts so i don’t need much space for serer , i can hotlink them, that’s way i can host 1tb and hot link via a 10gb vps.
    But the problem is file is saving by “” in this case .
    Can you help me by that?

    Thanks in advance.

    • Mithun John Jacob November 11, 2013 / 12:06 PM

      You can change the Line 4 to $file = "";

      In the current code, it just saves the file using filename in the file URL.

  2. Fauzi April 2, 2014 / 6:28 PM

    Thanks for Sharing

  3. vikram October 23, 2018 / 5:09 PM

    Wow, This is really helpful for me.
    Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *