通过php帖子下载.zip文件
试图通过一篇php文章,在点击一个按钮时显示一个文件的下载。目前,我正在传递一个数组,但没有在php中使用它,因为我想先让下载工作正常。我在firebug中看到的帖子得到了混乱的回复 当在浏览器中直接调用相同的php时,它可以正常工作,并按预期显示下载对话框 js: php:通过php帖子下载.zip文件,php,jquery,post,download,Php,Jquery,Post,Download,试图通过一篇php文章,在点击一个按钮时显示一个文件的下载。目前,我正在传递一个数组,但没有在php中使用它,因为我想先让下载工作正常。我在firebug中看到的帖子得到了混乱的回复 当在浏览器中直接调用相同的php时,它可以正常工作,并按预期显示下载对话框 js: php: 数据不是jquery的保留变量名吗?您是否会尝试更改var数据部分,以便它不会与返回后数据变量冲突?另外,是否愿意尝试在页面上的任何位置显示数据,以便javascript知道如何处理返回的对象?我在其他调用中使用ajax以
数据不是jquery的保留变量名吗?您是否会尝试更改var数据部分,以便它不会与返回后数据变量冲突?另外,是否愿意尝试在页面上的任何位置显示数据,以便javascript知道如何处理返回的对象?我在其他调用中使用ajax以相同的方式传递相同的数据,它工作得非常好。我认为这更像是一个下载问题。现在,正在传递的数据与任何事情都无关。您正在使用会话_start;,正当
$('#dtDownload').on('click', function () {
var data = {
'selected': selected
};
$.post("/process/p_screenshots_download.php", data, function(data, status){
});
});
<?php
require '/home/test/public_html/custom/functions/session.php';
// set screeshot directory
$directory = '/home/test/public_html/screenshot_files/'.$_SESSION['user']['account_id'].'/';
// open screenshort directory
$dir = opendir($directory);
// set temp directory
$tempdir = '/home/test/public_html/screenshot_files/'.$_SESSION['user']['account_id'].'/temp/';
//if temp directory does not exist then create it
if(!file_exists($tempdir))
{
mkdir($tempdir);
}
// start filenames at Screenshot_$i
$i = 1;
while($file = readdir($dir)) {
$dest = $tempdir.'Screenshot_'.$i.'.jpg';
if (preg_match("/(jpg)/i", $file))
{
copy($directory.$file, $dest);
$i++;
}
}
$now = time();
ob_start();
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream", FALSE);
header("Content-Type: application/download", FALSE);
header("Content-Disposition: attachment; filename=\"".$now."_Archive.zip\"");
header("Content-Transfer-Encoding: binary");
exec("zip -j ".$tempdir."archive.zip ".$tempdir."*.jpg 2>&1", $output);
$tempdirdel = opendir($tempdir);
while($tempfile = readdir($tempdirdel))
{
if (preg_match("/(jpg)/i", $tempfile))
{
unlink($tempdir.$tempfile);
}
}
ob_end_flush();
readfile($tempdir."archive.zip");
unlink($tempdir."archive.zip");
?>