我在Amazon S3上有一个带有签名网址的对象。我的示例被简化为一个图像,我的目标是下载带有该图像的zip文件。
此图片适用于变量$fileUrl
但不是这个(在变量$fileUrl
中),我得到一个损坏的.zip文件。我可以使用签名的网址在浏览器中打开图片。
$zipName = 'zipfile.zip';
# create new zip opbject
$zip = new ZipArchive();
$fileUrl = 'https://mybucketname.s3.amazonaws.com/94c70e8a-61f6-4f58-a4b7-1679df5a7c1f.jpg?response-content-disposition=attachment%3B%20filename%3D94c70e8a-61f6-4f58-a4b7-1679df5a7c1f.jpg&AWSAccessKeyId=AKIAJNY3GPOFFLQ&Expires=1426371544&Signature=HDNhGKHDsjEgqkMreN3tbozkfzo%3D';
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
# download file
$download_file = file_get_contents($fileUrl);
#add it to the zip
$zip->addFromString(basename($fileUrl),$download_file);
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($tmp_file));
readfile($tmp_file);