title: Download an Excel file from web server folder
body: <p>I am using PHPExcel to get Oracle SQL query results to an .xlsx file. I wrote my PHP code in <code>node--18769.tpl.php</code> and <code>node--18769.tpl.xlsx</code> file is downloading to webroot (/root/themes/bartik/templates) folder with result.</p>
  
  <p>My requirement:</p>
  
  <ol>
  <li><p>Can I rename <code>node--18769.tpl.xlsx</code> to <code>report.xlsx</code>?</p></li>
  <li><p>Is it possible to prepend <code>UNIX TIMESTAMP</code> to file name? (like 1442223364_report.xlsx)</p></li>
  <li><p>How can I download  <code>report.xlsx</code> to my local system once after the file is generated?</p></li>
  </ol>
  
  <p>This is my code:</p>
  
  <p><code>require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
      // Create new PHPExcel object
      $objPHPExcel = new PHPExcel();
      // Set document properties
      // Add some data
      $query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/( SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per FROM REPORT_LIST_VIEW WHERE DAYCOUNT &gt; '0' GROUP BY TITLE, PID, TYPE ORDER BY tot DESC";
      //print $query; exit;
      $res = db_query($query);
      $rowNumber = 1;
      while ( $dataFetched = $res-&gt;fetchAssoc() ) {
          $objPHPExcel-&gt;setActiveSheetIndex(0)
              -&gt;setCellValue('A'.$rowNumber, $dataFetched['title'])
              -&gt;setCellValue('B'.$rowNumber, $dataFetched['tot'])
              -&gt;setCellValue('C'.$rowNumber, $dataFetched['per']);
          $rowNumber++;
      }
      // Miscellaneous glyphs, UTF-8
      $objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(8)-&gt;setRowHeight(-1);
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A8')-&gt;getAlignment()-&gt;setWrapText(true);
      // Rename worksheet
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Page &amp; Files Reports ');
      // Set active sheet index to the first sheet, so Excel opens this as the first sheet
      $objPHPExcel-&gt;setActiveSheetIndex(0);
      $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A8',"Hello\nWorld");
      $objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(8)-&gt;setRowHeight(-1);
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A8')-&gt;getAlignment()-&gt;setWrapText(true);
      // Rename worksheet
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');
      // Set active sheet index to the first sheet, so Excel opens this as the first sheet
      $objPHPExcel-&gt;setActiveSheetIndex(0);
      // Save Excel 2007 file
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      $objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));</code></p>
  
  <p>Code updated:</p>
  
  <p><code>$objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');
      // Set active sheet index to the first sheet, so Excel opens this as the first sheet
      $objPHPExcel-&gt;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="01simple.xlsx"');
      header('Cache-Control: max-age=0');
      // If you're serving to IE 9, then the following may be needed
      header('Cache-Control: max-age=1');
      // If you're serving to IE over SSL, then the following may be needed
      header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
      header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
      header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
      header ('Pragma: public'); // HTTP/1.0
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      $objWriter-&gt;save('php://output');
      exit;</code></p>
  
  <p><a href="https://i.stack.imgur.com/iRs3D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iRs3D.png" alt="enter image description here"></a></p>