PHPExcel Zip 扩展名错误 - 无法生成 Excel 文件


PHPExcel Zip extension errors - can't produce Excel file

概述

我正在尝试直接下载使用PHPExcel创建的Excel电子表格。我没有服务器级访问权限,因此无法安装或启用模组(例如 Zip 模块)。

数据是事件的来宾列表。

法典

<?php
if(isset($_GET["event_id"])&&
    !empty($_GET["event_id"])){
    //Include PHPExcel, Excel2007, classes
    require_once("inc/PHPExcel/PHPExcel.php");
    require_once("inc/PHPExcel/PHPExcel/Writer/Excel2007.php");
    require_once("inc/classes.php");
    //Zip not installed - change settings to use local compression
    PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
    //Get event data
    $event_id = intval($_GET["event_id"]);
    $event = new Event($event_id);
    $guests = $event->getGuests();
    //Create new PHPExcel object
    $spreadsheet = new PHPExcel();
    //Add data
    $spreadsheet->setActiveSheetIndex(0);
    $spreadsheet->getActiveSheet()->SetCellValue("B2", "TMC Gateway");
    $spreadsheet->getActiveSheet()->SetCellValue("B3", "Event register");
    $spreadsheet->getActiveSheet()->SetCellValue("B5", "Name");
    $spreadsheet->getActiveSheet()->SetCellValue("C5", "Member/Guest");
    $spreadsheet->getActiveSheet()->SetCellValue("D5", "Checkin Time");
    foreach($guests as $guest){
        if($guest["degree"]=="guest"){
            $arr[] = [$guest["name1"]." ".$guest["name2"], "Guest", $guest["checkintime"]];
        } else {
            $arr[] = [trim($guest["name2"]), "Member", $guest["checkintime"]];
        }
    }
    $currentCell = 6;
    foreach($arr as $a){
        $spreadsheet->getActiveSheet()->SetCellValue("B$currentCell",$a[0]);
        $spreadsheet->getActiveSheet()->SetCellValue("C$currentCell",$a[1]);
        $spreadsheet->getActiveSheet()->SetCellValue("D$currentCell",$a[2]);
        $currentCell++;
    }
    //Rename sheet
    $spreadsheet->getActiveSheet()->setTitle("TMC Gateway");
    //Open writer
    $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
    //Set headers and force download
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;filename='"TMC_Gateway_Attendees-".$event_id.".xls'"");
    $writer->save("php://output");
    //Kill script
    exit;
}

问题

最初处理并打开文件时,我看到此错误:

致命错误:在/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007 中找不到类"ZipArchive.php第 227

我意识到这可能是因为 Zip 模块未安装或未启用,所以我在使用 PHPExcel 时按照类"ZipArchive"未找到错误中的这些说明进行操作:

如果您没有为 PHP 安装/启用 ZipArchive,并且无法自己启用它,那么您可以使用

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

但是,现在打开文件时,会出现此错误:

致命错误:未捕获的异常"PHPExcel_Writer_Exception",并显示消息"压缩文件时出错:PCLZIP_ERR_READ_OPEN_FAIL (-2):无法在二进制写入模式下打开临时文件"/tmppclzip-56df08ee0384c.tmp"在二进制写入模式下在/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108
堆栈跟踪:
#0/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php(278): PHPExcel_Shared_ZipArchive->addFromString('_rels/.rels', '<?xml version="...')
#1/home/loqui/public_html/doorapp/xls.php(66): PHPExcel_Writer_Excel2007->save('php://output')
#2 {主要}
扔在/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php108

问题

由于我没有启用 Zip 模块,并且工作文件夹中的权限似乎有限,因此如何使此脚本下载正确创建的 Excel 文件?

如果您打算继续使用 PCLZIP,我建议您检查它尝试写入的 tmp 目录,并查看该目录分配给哪个用户。Apache很可能没有对该tmp目录的写入权限,因此无法在其中写入文件。我在 ZipArchive 方面遇到了类似的问题,但如果您有足够的权限 chown -R user:user foldername 可能会缓解您的写入问题。