在一个 php 循环内执行另一个 php 文件

execute another php file within one php within loop

我有一个 php 代码,其中包含 if 语句(导出为 pdf,导出为 excel,最后一个要上传 excel)。

前两个 if 上传 pdf 和 excel 工作正常,当我点击 运行 第三个 if 语句的按钮时,它显示空白页面,即使代码因为当我单独 运行 它时它工作正常。

我的 php 代码如下:

<?php
define('FPDF_FONTPATH','/Applications/XAMPP/xamppfiles/lib/php'); 
if (isset($_POST['pdf'])) {
     //code//
}
}
if (isset($_POST['excel'])) {
    //code//

}

if (isset($_POST['upload'])) {

exec( 'imp.php');

}
?>

我正在尝试 运行 的 imp.php 文件是:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'mydatabase';
$table = 'demo';

if (!@mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");


    if(isset($_POST['submit']))
    {
         $fname = $_FILES['sel_file']['name'];
         echo 'upload file name: '.$fname.' ';
         $chk_ext = explode(".",$fname);

         if(strtolower(end($chk_ext)) == "csv")
         {

             $filename = $_FILES['sel_file']['tmp_name'];
             $handle = fopen($filename, "r");

             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
                $sql = "INSERT into demo(orders,quantity,country,dates,comment) values('$data[0]','$data[1]','$data[2]', '$data[3]','$data[4]')";
                mysql_query($sql) or die(mysql_error());
             }

             fclose($handle);
             echo "Successfully Imported";

         }
         else
         {
             echo "Invalid File";
         }   
    }

    ?>
    <h1>Import CSV file</h1>
    <form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post' enctype="multipart/form-data">
        Import File : <input type='file' name='sel_file' size='20'>
        <input type='submit' name='submit' value='submit'>
    </form

exec 函数在服务器上执行文件。除非您将 exec 的结果显式获取到变量并输出它,否则您将看不到任何内容。

但正如我所见 - 您只是想将一些 php 文件包含到另一个文件中 - 因为这是 includerequire 应该:

include '/path/to/file.php';

这就够了。