【问题标题】:PHPExcel Zip extension errors - can't produce Excel filePHPExcel Zip 扩展错误 - 无法生成 Excel 文件
【发布时间】:2016-06-22 18:50:30
【问题描述】:

概述

我正在尝试直接下载使用 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.php 中找不到类“ZipArchive”227

我意识到这可能是因为 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):无法以二进制写入模式打开临时文件'/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', ' #1 /home/loqui/public_html/doorapp/xls.php(66): PHPExcel_Writer_Excel2007->save('php://output')
#2 {主}
108

行的 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php 中抛出

问题

由于我没有启用 Zip 模块,而且工作文件夹中的权限似乎有限,我怎样才能让这个脚本下载正确创建的 Excel 文件?

【问题讨论】:

  • 似乎该类正在尝试在根目录中创建一个临时文件...也许您需要设置临时目录?
  • 您需要将脚本指向一个可由网络服务器写入的文件夹。实际上,您的脚本无法这样做。可以更改权限吗?你在用什么?

标签: php phpexcel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多