dongwo2772 2016-06-02 15:32
浏览 57
已采纳

如何从单独的PHP脚本执行PHP脚本而不会丢失第二个脚本的输出?

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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)