Laravel Zip档案无法到达网站

时间:2018-11-22 08:37:29

标签: php laravel laravel-5.4 ziparchive

我正在尝试下载pdf文件并使用ZipArchive将其压缩。但这给了我“无法访问此网站”错误。

这是我的代码:

$zip = new \ZipArchive();
$archive_file_name = date('YmdHis') . '.zip';

if ($zip->open($archive_file_name, $zip::CREATE) !== true) {
    exit("cannot open <$archive_file_name>\n");
}

foreach ($Contracts as $Contract) {

    if (!$Contract->parent_id) {
        $template = $request->template;
    } else {
        $template = $request->template_amendment;
    }

    $pdf = $Contract->getPdfPath($template);

    /** @var \Dompdf\Dompdf $dompdf */
    if (!$pdf && $dompdf = $Contract->generatePdf(true, true, $template)) $pdf = $dompdf->path;

    $ContractPdf = \App\Models\ContractPdf::where('contract_id', $Contract->id)->where('pdf_template', $template)->orderBy('created_at', 'desc')->first();
    $unprotected = str_replace($ContractPdf->pdf_filename, 'unprotected/' . $ContractPdf->pdf_filename, $pdf);

    if ($unprotected && file_exists($unprotected)) $pdf = $unprotected;

    $zip->addFile($Storage->url($pdf), $ContractPdf->pdf_filename);

}

$zip->close();

//then send the headers to force download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;

我的代码或本地设置中存在问题吗?以及如何解决?

1 个答案:

答案 0 :(得分:0)

案例已关闭!

我是多么愚蠢!我应该将$pdf放在addFile()参数中,而不是$Storage->url($pdf)