I am trying to implement download functionality in PHP. A user can download single or multiple files. If user try to download single file, the file gets downloaded normally. If user tries to download multiple files, all the files will be converted to zip and get downloaded.
But my problem is how to pass the gruop of filenames along with their paths. For a single file I passed it using
window.location = directories_path + "?action=download&files="+download;
download can be an array where I can pass files. But I am unable to pass them. My url appeared alike
localhost/proj/Base?action=download&files=[object Object]
I even tried with AJAX passing file names in json format. But it didn't work.
My JS code for the download process
$("input:checked").each(function(){
if($(this).siblings('a').length > 0)
{
download.push({
name: $(this).siblings('a').text(),
path: $(this).siblings('a').attr('href'),
});
}
});
if( checked == 0 ) {
alert("Please select any file to download");
} else {
window.location = directories_path + "?action=download&files="+download;
}
My php code for downloading single file is
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
readfile($file);
My question is Is there any way so that I can pass an array of objects(filename and filepath) in url so that I can download the files correspondingly. Thanks