Save & Copy

Topics: Developer Forum
Dec 10, 2010 at 4:44 AM

I have only been working with this version 1.7.4 for a short time and I find extremely useful, however there is 1 thing that I cannot seem to manage.  When creating an excel or pdf file for the user to download, I want to ALSO store a copy on the server.  I have tried a number of things in the final few lines of the code (below), but cannot get it to work.  Is there anyone or any way to do this?  Thanks!

// Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle('Export Sheet');
       
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
 
        // Redirect output to a client's web browser (Excel2007)       
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$Temp_Export_File_Name.'"');
        header('Cache-Control: max-age=0');
       
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;

Coordinator
Dec 10, 2010 at 10:45 PM

Instead of

$objWriter->save('php://output');

which outputs directly to the PHP output stream

use

$objWriter->save('./saveFile.xlsx');
readfile('./saveFile.xlsx');

which saves the Excel2007 document to a file on the server, then spools the content of that file to the PHP output stream

Dec 11, 2010 at 9:20 AM
MarkBaker, Thank you! That was exactly what I was looking for. I knew the readfile would come into it, but just didn't know what I was missing. Again, thank you.