RC1140
RC1140

Reputation: 8653

Download then upload a file in PHP

I need to download a file using PHP and then after that upload the downloaded file to the user.

e.g.

  1. Download file from test.com/test.zip

  2. Either cache the file to disk or stream Immediately to the user.

If possible how can i do this using curl or how can i do this using normal PHP (no extra libs)

Upvotes: 1

Views: 997

Answers (2)

Ciaran McNulty
Ciaran McNulty

Reputation: 18848

Using echo(file_get_contents()) will load the file into memory first, then echo will output it to the user - there is therefore a delay before file delivery to the user starts, and you risk hitting PHP's memory limit.

In contrast readfile() will serve chunks to the user as soon as they're downloaded from the remote source, then throw them away when they've been received. It will use far less memory and the download will start quicker.

header('Content-Type: application/zip');
readfile('http://test.com/test.zip');

Upvotes: 4

Matthew Scharley
Matthew Scharley

Reputation: 132234

header("Content-Type: application/zip"); // correct? not sure.
echo(file_get_contents("http://test.com/test.zip"));

Unless I missed something that you want to do... If you want to cache it, you can seperate the calls, and store the results of file_get_contents somewhere. The result of this call is the file as a string. echo just prints it back out again to the browser. header is used to tell the browser that this file is actually a zip file, not a html file.

Upvotes: 3

Related Questions