我的问题是:我使用PHP脚本来压缩文件夹并下载它。但我是通过AJAX-jQuery代码来实现的。因此,当我单击按钮生成ZIP包时,它将无法正常工作。但是当我启用IDM(Internet Download Manager)时,它就会被下载。但是在禁用创建的IDM ZIP文件但无法下载之后。我已经附加了在Chrome控制台上录制的AJAX响应。
这是我的PHP函数:
public function createZip($files, $zip_file_name) {
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
if(count($valid_files > 0)){
$zip = new ZipArchive();
$zip_name = $zip_file_name.".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($valid_files as $file){
$zip->addFile($file);
}
$zip->close();
if(file_exists($zip_name)){
// force to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file from temp path
unlink($zip_name);
return "Files are successfully Archived !";
}
else{
return "ERROR in ZIP FIle Archive!";
}
} else {
return "No valid files to zip";
exit;
}
}
这是我的jQuery / AJAX代码:
$(document).on('click', '.zip_dl', function(){
var click_id = $(this).attr('id');
//alert(click_id);
$.ajax({
url: 'includes/AjaxResponses.php',
data: {
folder_name : click_id,
action_type : "zip_file_download"
},
error: function() {
//$('#info').html('<p>An error has occurred</p>');
},
dataType: 'JSON',
success: function(response) {
},
type: 'GET'
});
});