Search code examples
excellaravelphpoffice

Error while creating Excel file using PHPOffice/SpreadSheet (ZipArchive::close())


I am using PHPOffice/SpreadSheet in Laravel PHP framework. My computer operating system is macOS.

While generating excel file I receive this error

ZipArchive::close(): Failure to create temporary file: No such file or directory

My full code is here

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
class SpreadSheetController extends Controller
{
    public function examMarksEntrySheet(Request $request)
    {
        $reader = new Xlsx();
        $spreadSheet = $reader->load($inputFileName);
        $spreadSheet->setActiveSheetIndex(0);
        $activeSheet = $spreadSheet->getActiveSheet();
        // loaded excel file is edited here (hidde)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
        $writer->save('php://output');
    }
}



Laravel Version: 5.5
PHP Version: 7.1.7
PHPOffice/PHPSpreadSheet Version: 1.2
macOS Version: 10.13.3


Solution

  • Successfully solved the issue using \PhpOffice\PhpSpreadsheet\Shared\File::setUseUploadTempDirectory(true);
    Now everything working very well.

    The issue was the tmp directory used by phpSpreadSheet was not writable by PHP process. Changed the tmp directory to upload tmp directory and now the issue is resolved.