Reputation: 2599
I have multiple files stored in my server which they can be found with userid and their address in my table.
they have different file types like docx and jpeg and pdf.
I have a jquery post method which will send user_id , to the proper php file which in it I will run my query to get users files addresses as an array.
$("#download_btn").click(function () {
var uni_id = $("#uni_selection_filter").val();
$.post("Requests/zipCorsponds.php", //Required URL of the page on server
{ // Data Sending With Request To Server
Get_arch_letters : true,
user_id : vuser_id,
user_role : "<?php echo $_SESSION["role"]; ?>",
section_num : "<?php echo $_SESSION['section_id'] ?>",
uni_id : uni_id
},
function(response){ // Required Callback Function
$("#dynamic_table").html(response);
});
});
Now I have added below method after the query to compress those files and send the result to view which is jquery that user be able to download all his/her files.
if ($_POST['Get_arch_letters']) {
$user_id = $_POST['user_id'];
$user_role = $_POST['user_role'];
$section_num = $_POST['section_num'];
$uni_id = $_POST['uni_id'];
if ($user_role == 'executive_manager') {
$load_corspnd_letter = $DBM->RunQuery("SELECT at_corspond_file.corspond_file FROM at_corspnd
INNER JOIN at_corspond_file ON (at_corspnd.id = at_corspond_file.corspond_id)
WHERE at_corspnd.uni_id = '$uni_id' ", true, false);
}
$files = mysqli_fetch_assoc($load_corspnd_letter);
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = 'Resume.zip';
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) { die($file.' does not exist'); }
if (!is_readable('../'.$file)) { die($file.' not readable'); }
# download file
$download_file = file_get_contents('../'.$file);
#add it to the zip
$zip->addFile(basename($file),$download_file);
}
# close zip
$zip->close();
echo 'Requests/'.$tmp_file;
}
but what I'm getting in result is : PK|H�IPK|H�I��PK.
how can I accomplish this ?
Update
I've added conditions to check if the archive file is created or not, it passes this condition but there is no file in my directory :
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = 'Resume.zip';
if($zip->open($tmp_file, \ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true)
echo 'cannot create zip';
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) {
continue;
}
if (!is_readable('../'.$file)) { die($file.' not readable'); }
#add it to the zip
$zip->addFile(basename($file));
}
# close zip
$zip->close();
echo 'Requests/'.$tmp_file;
Upvotes: 3
Views: 45
Reputation: 1163
You can not download the file with your approach. Right now your code tries to display the result of your call to the server. You have 2 options to make this work.
1. Using your current php code
The php code would work if called directly in the browser. You can use your '#download_btn'
to submit a form with the proper input values
HTML
<form action="Requests/zipCorsponds.php" method="post" id="frm">
<input type="hidden" name="Get_arch_letters" value="true" />
<input type="hidden" name="user_role" value="<?php echo $_SESSION["role"]; ?>" />
...
<input type="hidden" name="uni_id" id="uni_id" value="" />
</form>
jquery
$("#download_btn").click(function () {
var uni_id = $("#uni_selection_filter").val();
$("#uni_id").val(uni_id);
$("#frm").submit();
});
.
2. Using your current jquery post approach
You would need to change the callback function to download the returned file.
jquery
...
function(response){ // Required Callback Function
window.location = response;
});
...
And to change the php code to return the file path and not the file itself. You also need to name the file proper, with the correct extension.
php
...
# create a file & open it
$tmp_file = 'Resumes.zip';
$zip->open($tmp_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) { die($file.' does not exist'); }
if (!is_readable('../'.$file)) { die($file.' not readable'); }
#add file to the zip
$zip->addFile(basename('../'.$file));
}
# close zip
$zip->close();
# send the file to the post request
echo 'Requests/'.$tmp_file;
Upvotes: 2