使用zend框架进行多个pdf下载

时间:2013-01-07 07:52:04

标签: zend-framework pdf

我将这个代码附加到我的网站上,以便观众下载这样的pdf文件..

$ fullPath =“../ public / pdffiles / FolioPlusUserGuide(v3.0).pdf”;

    if ($fd = fopen ($fullPath, "r")) {

        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);

        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");            
        header("Content-length: $fsize");
        header("Cache-control: private");

        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }

    fclose ($fd);
    exit;

html代码:

baseUrl()。'/ DownloadPdf'?>“> DOWNLOAD BROCHURE

这段代码只能容纳1个文件路径,我有更多不同文件名的pdf文件和每个处理下载事件的不同按钮。我怎么能做到这一点?...感谢未来!.. =)

1 个答案:

答案 0 :(得分:0)

您可以通过HTTP请求传递PDF文件名,如下所示:

DownloadPDF.php?文件名= test1.pdf

然后在你的DownloadPDF文件中有这样的东西:

$pdfdir = "/pdffiles/folder/location/here";
$pdffilename = $_GET['filename'];
$fullPath = $pdfdir.$pdffilename;

if ($fd = fopen ($fullPath, "r")) {

        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);

        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");            
        header("Content-length: $fsize");
        header("Cache-control: private");

        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }

    fclose ($fd);
    exit;

现在您的代码是动态的,您可以下载任何带有以下链接的PDF:

<a href=downloadpdf.php?filename=something.pdf>Something</a>
<a href=downloadpdf.php?filename=something2.pdf>Something2</a>