简体   繁体   中英

PHPExcel Zip extension errors - can't produce Excel file

Overview

I'm trying to directly download an Excel spreadsheet created using PHPExcel. I don't have server-level access so I can't install or enable mods (such as the Zip module).

The data is a guestlist for an event.

Code

<?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;
}

Issue

When processing originally and opening the file, I saw this error:

: Class 'ZipArchive' not found in on line :在第行的找不到类“ ZipArchive”

I realised this is probably because the Zip module was either not installed or not enabled, so I followed these instructions at Class 'ZipArchive' not found error while using PHPExcel :

If you don't have ZipArchive installed/enabled for your PHP, and can't enable it yourself, then you can use

 PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP); 

However, now when opening the file, this error appears:

: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Error zipping files : PCLZIP_ERR_READ_OPEN_FAIL (-2) : Unable to open temporary file '/tmppclzip-56df08ee0384c.tmp' in binary write mode' in /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108 :带有消息“错误压缩文件:PCLZIP_ERR_READ_OPEN_FAIL(-2)”的未捕获异常“ PHPExcel_Writer_Exception”:/ home / loqui / public_html / doorapp / inc无法在二进制写入模式下打开临时文件“ /tmppclzip-56df08ee0384c.tmp” /PHPExcel/PHPExcel/Shared/ZipArchive.php:108
Stack trace:
#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}
thrown in on line 行的抛出

Question

As I don't have the Zip module enabled, and seemingly limited permissions in the working folder, how can I make this script download the correctly created Excel file?

If you are going to continue using PCLZIP, I would recommend checking the tmp directory that it is attempting to write to and see what user that directory is assigned to. More than likely Apache does not have write access to that tmp directory and thus is failing to write files inside of it. I am struggling with a similar problem on the ZipArchive front, but if you have sufficient permissions chown -R user:user foldername might alleviate your write issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM