tags:

views:

79

answers:

2

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)

+3  A: 
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.

Matthew Scharley
Thanks that seems exactly what i need as soon as i test it will mark as complete
RC1140
+3  A: 

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');
Ciaran McNulty