在 Ajax 中定义 Header 以生成 zip PHP

define Header in Ajax for generating zip PHP

我正在尝试使用 ZipArchive Class 生成 zip 文件,其中包含也使用 FPDF 库生成的 pdf 文件。

我正在使用 ajax 和 PHP 来做到这一点,问题是我不知道如何在 ajax 中设置 Headers 来强制在浏览器中下载。 文件内容在我的 HTML 中读取,而不是发送回 zip 下载。

PHP 脚本:

<?php
require('config/config.php');
require('fpdf/fpdf.php');

if(!isset($_SESSION))
{
    session_start();
}

$output = "";

if(isset($_SESSION['user']))
{
    $currentTime = time();
    if($currentTime < $_SESSION['expire'])
    {
        if(!empty($_POST['candidacy']))
        {
            $stmt = $cnx->prepare('SELECT * FROM candidacies WHERE id = :candidacy');
            $stmt->bindValue(':candidacy', $_POST['candidacy'], PDO::PARAM_INT);
            $stmt->execute();

            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            var_dump($result);
            foreach($result as $key => $value)
            {
                $nameUser = $value['lastName']." ".$value['firstName'];
                $emailUser = $value['email'];
            }

            $pdf = new FPDF();

            $pdf->AliasNbPages();
            $pdf->AddPage();

            $pdf->Image('img/candidacy.jpg',10,6,25);

            $pdf->SetFont('Times','',13);

            $pdf->Cell(60);
            $pdf->Cell(80,10, utf8_decode('Candidacy'),1,1,'C');
            $pdf->ln();

            $pdf->Cell(60);
            $pdf->Cell(80,10, date("d/m/Y"),1,1,'C');
            $pdf->ln();

            $pdf->Cell(50,10,'Nom complet : ', 0,0);
            $pdf->Cell(65,10, utf8_decode($nameUser), 0,1);

            $pdf->Cell(50,10, 'Email : ', 0,0);
            $pdf->Cell(100,10, utf8_decode($emailUser), 0,1);

            $file = 'CCandidacy-'.$nameUser.'.pdf';

            $pdf->Output('F', $file);

            if(extension_loaded('zip'))
            {
                $zip = new ZipArchive();

                $zip_name = md5(random_bytes(64)).".zip";

                if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
                {
                    $output .= "Impossible to create zip !";
                }

                $zip->addFile($file);

                $zip->close();

                if(file_exists($zip_name))
                {
                    header('Content-type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name);
                    header('Content-Length: ' . filesize($zip_name));

                    readfile($zip_name);

                    unlink($zip_name);
                }
            }
            else
            {
                $output .= "Check extension !";
            }

            return $zip_name;
        }
    }
}
else
{
    unset($_SESSION['user']);
    session_destroy();
    header('Location: ../login.php');
    exit(0);
}

JS 脚本:

function zipExtract(candidacy)
{
    var content = {};
    content['candidacy'] = candidacy;

    $.post('zipCandidacy.php', content, function(data)
    {
        location.href = data;
    });
}

一旦 ajax 调用准备就绪,您可以使用 location.href 重定向到生成的 zip 文件(这意味着您的 zip 文件也已准备就绪)

Return 来自您 PHP 的 zip 文件的 url 然后只需使用下面的...

function zipExtract(candidacy)
{
    var content = {};
    content['candidacy'] = candidacy;

    $.post('zipCandidacy.php', content, function(data)
    {
       location.href=data;
    });
}

我认为您不需要以下内容,但为了以防万一...

请记住,您不要在 ajax 中设置 headers。您使用 header() 函数在 php 文件中设置 headers http://php.net/manual/en/function.header.php

zip 的 mime 类型(如果服务器不能满足您的 zip 请求,则直接下载)

application/zip, application/octet-stream

在 PHP 代码中..

// We'll be outputting a ZIP
header('Content-Type: application/zip, application/octet-stream');

// It will be called myzip.zip
header('Content-Disposition: attachment; filename="myzip.zip"');

// your PHP code and FPDF stuff

我终于找到了解决办法!正如你向我指出的那样,Oliver,AJAX 并不是我想要的定义 header 的最佳方式。因此,当执行 onclick 事件时,我重定向到 php 处理页面,以便继续下载 zip 文件!

<?php
require('config/config.php');
require('fpdf/fpdf.php');

if(!isset($_SESSION))
{
    session_start();
}

$output = "";

if(isset($_SESSION['user']))
{
    $currentTime = time();
    if($currentTime < $_SESSION['expire'])
    {
        if(!empty($_GET['candidacy']))
        {
            $stmt = $cnx->prepare('SELECT * FROM candidacies WHERE id = :candidacy');
            $stmt->bindValue(':candidacy', $_GET['candidacy'], PDO::PARAM_INT);
            $stmt->execute();

            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

            foreach($result as $key => $value)
            {
                switch($value['zipcode'])
                {
                    case "North":
                        $zipcode = "Nord";
                        break;
                    case "South":
                        $zipcode = "Sud";
                        break;
                    case "East":
                        $zipcode = "Est";
                        break;
                    case "West":
                        $zipcode = "Ouest";
                        break;
                }

                switch($value['transportMeans'])
                {
                    case "onFoot":
                        $transportMeans = "À pied";
                        break;
                    case "vehicle":
                        $transportMeans = "Voiture";
                        break;
                    case "bike":
                        $transportMeans = "Moto";
                        break;
                    case "bus":
                        $transportMeans = "Bus";
                        break;
                    case "carpool":
                        $transportMeans = "Covoiturage";
                        break;
                    case "bicycle":
                        $transportMeans = "Vélo";
                        break;
                }

                switch($value['candidacyType'])
                {
                    case "mysteryCustomer":
                        $candidacyType = "Client Mystère";
                        break;
                    case "investigationUnit":
                        $candidacyType = "Chargé d'enquêtes";
                        break;
                    case "investigators":
                        $candidacyType = "Enquêteurs/Enquêtrices";
                        break;
                }

                $nameUser = $value['lastName']." ".$value['firstName'];
                $age = $value['age'];
                $employment = $value['employment'];
                $zipcode = $zipcode;
                $transportMeans = $transportMeans;
                $scheduleRange = $value['scheduleRange'];
                $emailUser = $value['email'];
                $phoneNumber = $value['phoneNumber'];
                $candidacyType = $candidacyType;
                $coveringLetter = $value['coveringLetter'];
                $curriculumVitae = $value['curriculumVitae'];
                $createdAt = $value['createdAt'];
            }

            $pdf = new FPDF();

            $pdf->AliasNbPages();
            $pdf->AddPage();

            $pdf->Image('img/candidacy.jpg',10,6,25);

            $pdf->SetFont('Times','',13);

            $pdf->Cell(60);
            $pdf->Cell(80,10, utf8_decode('Candidature'),1,1,'C');
            $pdf->ln();

            $pdf->Cell(60);
            $pdf->Cell(80,10, $createdAt,1,1,'C');
            $pdf->ln();

            $pdf->Cell(50,10, 'Nom complet : ', 0,0);
            $pdf->Cell(65,10, utf8_decode($nameUser), 0,1);

            $pdf->Cell(50,10, utf8_decode('Âge : '),0,0);
            $pdf->Cell(65,10, utf8_decode($age), 0,1);

            $pdf->Cell(50,10,'Profession : ',0,0);
            $pdf->Cell(65,10, utf8_decode($employment), 0,1);

            $pdf->Cell(50,10, 'Secteur : ',0,0);
            $pdf->Cell(65,10, utf8_decode($zipcode), 0,1);

            $pdf->Cell(50,10, 'Moyen de transport :',0,0);
            $pdf->Cell(65,10, utf8_decode($transportMeans), 0,1);

            $pdf->Cell(50,10, 'Horaires : ',0,0);
            $pdf->Cell(65,10, utf8_decode($scheduleRange), 0,1);

            $pdf->Cell(50,10, 'Email : ', 0,0);
            $pdf->Cell(100,10, utf8_decode($emailUser), 0,1);

            $pdf->Cell(50,10, utf8_decode('N° de téléphone : '),0,0);
            $pdf->Cell(65,10, utf8_decode($phoneNumber), 0,1);

            $pdf->Cell(50,10, 'Candidature : ',0,0);
            $pdf->Cell(65,10, utf8_decode($candidacyType), 0,1);

            $file = 'Candidature-'.$nameUser.'.pdf';

            $pdf->Output('F', $file);

            $files = array($file, "candidatures/".$coveringLetter, "candidatures/".$curriculumVitae);

            if(extension_loaded('zip'))
            {
                $zip = new ZipArchive();

                $zip_name = md5(random_bytes(64)).".zip";

                if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
                {
                    $error .= "Création impossible du zip !";
                }

                foreach($files as $content)
                {
                    $zip->addFile($content);
                }

                $zip->close();

                if(file_exists($zip_name))
                {
                    header('Content-Type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name);
                    header('Content-Length: ' . filesize($zip_name));

                    readfile($zip_name);

                    unlink($zip_name);
                }
            }
            else
            {
                $error .= "Vérifier l'extension !";
            }

            $query = $cnx->prepare('UPDATE candidacies SET lastExport = now() WHERE id = :candidacy');
            $query->bindValue(':candidacy', $_GET['candidacy'], PDO::PARAM_INT);
            $query->execute();
        }
    }
}
else
{
    unset($_SESSION['user']);
    session_destroy();
    header('Location: ../login.php');
    exit(0);
}


function zipExtract(candidacy)
{
    window.location.href = ("../zipCandidacy.php?candidacy="+candidacy);
}