I am currently working on a project that uses HTML and PHP to allow a user to enter information into a web form in order to generate an Excel file. On submission, the form runs a PHP file, main.php:
<?php
// output headers so that the file is downloaded rather than displayed
$order = $_POST["order"];
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$_POST['order'].'.xls"');
exec('php xl.php');
sleep(2);
readfile('xl_template.xls');
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
where "xl.php" is another PHP file and "xl_template" is a template for the Excel sheet I wish to modify. The purpose of main.php is to grab the modified template and download it to the user's computer, while xl.php actually modifies the Excel template and saves it to the server computer (using PHPExcel library):
<?php
// this file will be called by main.php
// after execution, there should be a newfile.xls
// for the main.php to read from
error_reporting(E_ALL);
require('Classes/PHPExcel.php');
// variable definitions
$template = "xl_template.xls";
$objPHPExcel = PHPExcel_IOFactory::load($template);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->getCell('A2')->setValue('123400000000000000000001');
$objWorksheet->getCell('B2')->setValue('it worked!');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($template);
?>
This works as expected when the xl.php file is called from terminal and then the form is filled out. However, when xl.php is called from main.php through the exec('php xl.php')
method, the Excel file does not get updated, which I assume to mean that xl.php was not successfully executed.
In addition to exec()
, I have tried system()
,shell_exec
, the backtick operator, and passthru()
, with the same results. Also, I have tried Javascript and JQuery methods, calling the xl.php file with $.get('xl.php')
and $.ajax({url: 'xl.php'})
with no luck.
Any insight into this problem would be greatly appreciated, as I am still new to using PHP. Thanks.