通过php帖子下载.zip文件

时间:2014-01-18 00:37:41

标签: php jquery post download

尝试通过php帖子点击按钮来下载文件。目前我正在传递数组,但我没有在php中使用它,因为我想先让下载工作。对于在firebug中显示的帖子,我得到了一个混乱的回应。

直接在浏览器中调用相同的php工作正常,并按预期显示下载对话框。

JS:

$('#dtDownload').on('click', function () {

    var data = {
      'selected': selected
    };

    $.post("/process/p_screenshots_download.php", data, function(data, status){
    }); 
});

PHP:

<?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");

?>

0 个答案:

没有答案