2016-06-02 15:32
浏览 57


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:

    // 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');
    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):

    // this file will be called by main.php
    // after execution, there should be a newfile.xls
    // for the main.php to read from

    // variable definitions
    $template = "xl_template.xls";

    $objPHPExcel = PHPExcel_IOFactory::load($template);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $objWorksheet->getCell('B2')->setValue('it worked!');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

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.

图片转代码服务由CSDN问答提供 功能建议

我目前正在开发一个使用HTML和PHP的项目,允许用户将信息输入到Web表单中 以生成Excel文件。 提交时,表单运行一个PHP文件,main.php:

 $  order = $ _POST [“order”]; 
 header('Content-Type:text / plain; charset = utf-8'); 
 header('Content-Disposition:attachment; filename =“'。$ _ POST [  'order']。'。xls“'); 
 exec('php xl.php'); 
 error_reporting(E_ALL); \  n ini_set(“display_errors”,1); 

其中“xl.php”是另一个PHP文件,“xl_template”是一个模板 我想修改的Excel表格。 main.php的目的是获取修改后的模板并将其下载到用户的计算机,而xl.php实际修改Excel模板并将其保存到服务器计算机(使用PHPExcel库):

 //为main.php读取\  n error_reporting(E_ALL); 
 require('Classes / PHPExcel.php'); 
 $ template =“xl_template.xls”; 
 $ objPHPExcel = PHPExcel_IOFactory :: load(  $ template); 
 $ objWorksheet = $ objPHPExcel-&gt; getActiveSheet(); 
 $ objWorksheet-&gt; getCell('A2') - &gt; setValue('123400000000000000000001'); 
 $ objWorksheet-&gt; getCell(  'B2') - &gt; setValue('it working!'); 
 $ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel5'); 
 $ objWriter-&gt; save($ template); 
?  &gt; 

当从终端调用xl.php文件然后表单为fil时,这可以正常工作 领导了。 但是,当从main.php通过 exec('php xl.php')方法调用xl.php时,Excel文件不会更新,我认为这意味着xl.php是 没有成功执行。

除了 exec()之外,我还试过 system() shell_exec ,反引号运算符和 passthru(),结果相同。 此外,我尝试了Javascript和JQuery方法,使用 $。get('xl.php') $。ajax({url:'xl.php')调用xl.php文件。 没有运气。

对于这个问题的任何见解将不胜感激,因为我仍然是使用PHP的新手。 谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dsai1991 2016-06-02 15:50

    I would suggest making two separate functions and calling them in the order you need to to prevent the error from occurring. This is the most efficient way to do this.

    If you really want to use a different page, you could could possibly use the header() function in php to redirect to the second php page, and include any variables you need to pass in the URL and retrieve them using $_GET().

    解决 无用
    打赏 举报

相关推荐 更多相似问题