php - Not being able to save files inside ZIP with ZipArchive
Get the solution ↓↓↓I Have been reading many similar problems but I just can't find the solution to mine.
All I need is zip the files that come in the POST variable as an array and get the path to the zip file just created. Here is my code... I keep getting an empty Object and just can't make 1 file inside
function downloadAllImages(){
$data = array();
$files = $_POST['files_to_download'];
$archive_file_name = 'images.zip';
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
$data['debug'].=("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($files as $file)
{
$zip->addFile($file, basename($file));
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
$data['debug'].=print_r($zip,true);
echo json_encode($data);
die();
}
I'm using JQuery to get the download link after the file is zipped. I just can't make the files to zip.
The array files_to_download prints:
Array(
[0] => http://localhost/br/wp-content/uploads/2020/04/rabbit-black-transp-300x300.png
[1] => http://localhost/br/wp-content/uploads/2020/04/bg-shop-300x169.png
)
And my jQuery
function downloadAllImages(){
var files_to_download = [];
$("img.downloadable").each(function(){
files_to_download.push($(this).attr('src'));
});
jQuery.ajax({
url: runAJAX.ajaxurl,
data: ({action: 'downloadAllImages', files_to_download:files_to_download}),
method: "POST",
success: function(data_received) {
displayAjaxResponse(data_received);
}
});
}
Answer
Solution:
In the end a friend of mine helped and the PHP code should look like this:
function downloadAllImages()
{
$data = array();
$files = $_POST['files_to_download']; // array
$domain= wp_upload_dir();
// used this to rename the file so each post gets it's own zip
$prefix=$_POST['file_prefix'];
$zip = new ZipArchive();
$tmp_zip = $zip->open($domain['basedir'].'/'.$prefix.'images.zip', ZipArchive::CREATE);
foreach($filesas $img)
{
if(!empty($img))
{
$download_file = file_get_contents($img);
$zip->addFromString(basename($img), $download_file);
}
}
$zip->close();
$now = new DateTime();
$data['file'] = get_bloginfo('wpurl')."/wp-content/uploads/{$prefix}images.zip?v={$now ->getTimestamp()}";
echo json_encode($data);
die();
}
And the jQuery looks pretty much the same, except for adding the 'file_prefix' variable.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: constant expression contains invalid operations
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.