PHPExcel - instead of downloading save it to the directory












1















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?










share|improve this question



























    1















    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?










    share|improve this question

























      1












      1








      1








      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?










      share|improve this question














      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?







      php phpexcel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 12:59









      PabloPablo

      657




      657
























          2 Answers
          2






          active

          oldest

          votes


















          1















          1. Remove all header()'s

          2. Use full path's: e.g /home/database/public_html/application/mail/

          3. Cron job: Remove 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.






          share|improve this answer































            0














            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);) .






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447180%2fphpexcel-instead-of-downloading-save-it-to-the-directory%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1















              1. Remove all header()'s

              2. Use full path's: e.g /home/database/public_html/application/mail/

              3. Cron job: Remove 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.






              share|improve this answer




























                1















                1. Remove all header()'s

                2. Use full path's: e.g /home/database/public_html/application/mail/

                3. Cron job: Remove 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.






                share|improve this answer


























                  1












                  1








                  1








                  1. Remove all header()'s

                  2. Use full path's: e.g /home/database/public_html/application/mail/

                  3. Cron job: Remove 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.






                  share|improve this answer














                  1. Remove all header()'s

                  2. Use full path's: e.g /home/database/public_html/application/mail/

                  3. Cron job: Remove 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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 9:42









                  proofzyproofzy

                  495614




                  495614

























                      0














                      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);) .






                      share|improve this answer




























                        0














                        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);) .






                        share|improve this answer


























                          0












                          0








                          0







                          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);) .






                          share|improve this answer













                          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);) .







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 13:23









                          Daniel KollerDaniel Koller

                          262




                          262






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447180%2fphpexcel-instead-of-downloading-save-it-to-the-directory%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini