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

问题描述 投票:0回答:1

概观

我正在尝试直接下载使用PHPExcel创建的Excel电子表格。我没有服务器级访问权限,因此我无法安装或启用mod(例如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;
}

问题

在最初处理并打开文件时,我看到了这个错误:

致命错误:第227行/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php中未找到类“ZipArchive”

我意识到这可能是因为Zip模块未安装或未启用,所以我在Class 'ZipArchive' not found error while using PHPExcel上按照这些说明操作:

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

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

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

致命错误:未捕获异常'PHPExcel_Writer_Exception',消息'错误压缩文件:PCLZIP_ERR_READ_OPEN_FAIL(-2):无法在/ home / loqui / public_html / doorapp / inc中以二进制写入模式打开临时文件'/tmppclzip-56df08ee0384c.tmp' /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 {main} 在第108行的/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php中引用

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

php phpexcel
1个回答
0
投票

如果您要继续使用PCLZIP,我建议您检查它尝试写入的tmp目录,并查看该目录分配给哪个用户。 Apache很可能没有对该tmp目录的写访问权,因此无法在其中写入文件。我正在努力解决ZipArchive前端的类似问题,但是如果你有足够的权限chown -R user:user foldername可能会减轻你的写入问题。

© www.soinside.com 2019 - 2024. All rights reserved.