tim peterson
tim peterson

Reputation: 24305

Get zip file from url (PHP)

I have a zip files that I want users to be able to download. The trick is I don't want the users to see what the url is and I don't want to download the file to my server.

So I want users to click a link like this:

http://example.com/download/4

which server-side accesses my S3 bucket with this url:

https://s3.amazonaws.com/my-bucket/uploads/4.zip

I've tried cURL, using S3 methods, and various headers() in my download($file_id) function but can't get this to work. This has to be easy, right?

Upvotes: 0

Views: 1200

Answers (2)

xate
xate

Reputation: 6379

Your right, its quite easy. Probably you will have to write something like this:

$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();

You set various headers to make your user download the .zip. Afterwards you put your file into the output buffer with readfile() Afterwards you end your script with exit() for security's sake. This should work for you! Remember to change the path to your file.

Upvotes: 2

tim peterson
tim peterson

Reputation: 24305

Thanks @Xatenev for the help. This is actually what worked for me:

$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/zip"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
readfile($path); // outputs the content of the file

exit();

Upvotes: 0

Related Questions