Reputation: 113
I'm trying to store Excel generated file to server directory instead of downloading after reaching path.
My code:
// worksheet
$objPHPExcel->getActiveSheet()->setTitle('Podaci');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="mjesecni_podaci.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save('php://output');
$putanja_spremanja_fajla = 'mail/mjesecni_podaci.xlsx';
$objWriter->save($putanja_spremanja_fajla);
sleep(1);
slanje_maila($db, $datum);
I tried to remove some of header lines, but then my file is corrupted or I get some of errors. Any suggestions?
Upvotes: 1
Views: 2171
Reputation: 637
header()
's/home/database/public_html/application/mail/
if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser');
because this line of code checks if script is "called" by server(cgi-fcgi) or user(cli).After that, you can use cron jobs or similar (if sever trying to run .php script).
If you trying to reach link trough web browser, you must use headers because headers
talk with browser.
Upvotes: 1
Reputation: 26
the following lines tell the browser to expect a file. (The Excelfile mjesecni_podaci.xlsx to be exact)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="mjesecni_podaci.xlsx"');
If you tell PHPExcel to save the file to disk, no file is delivered to the browser. If there is no output at all or some kind of echo or something else, you will either get an Error-Message or the browser "downloads" the file "mjesecni_podaci.xlsx" with the output you produced.
For example:
<?php
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="mjesecni_podaci.xlsx"');
echo "Hello World!"
?>
This code will download the File "mjesecni_podaci.xlsx" with the content "Hello World!". (Save the file to disk and open it with an editor like Notepad++ or similar)
In your code, you should just remove the two lines, and make sure that your program can write to the subfolder "mail". Your code should work after that. (at least the part before sleep(1);
) .
Upvotes: 0