简体   繁体   English

使用exec()执行外部php文件

[英]execute external php file using exec()

i am new to exec function and need help with executing an external php file. 我是exec函数的新手,在执行外部php文件时需要帮助。 My code of the files are as follows 我的文件代码如下

Script.php(main file) : Script.php(主文件):

<?php 
$path = 'C:/xampp/htdocs/user/execute.php';
exec($path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>

execute.php(calling file) : execute.php(调用文件):

for($i=0;$i<10;$i++){
echo "hello"."<br>";
}

trying to execute the calling file 试图执行调用文件

exec is for executing system functions, not for running scripts. exec用于执行系统功能,而不用于运行脚本。 (Take a look at the manual, it's helping: http://php.net/manual/de/function.exec.php ) (看看手册,它对您有帮助: http : //php.net/manual/de/function.exec.php

To achieve what you want, you could pass the path to php executable and add the script as parameter, like this: 要实现所需的功能,可以将路径传递到php可执行文件,并将脚本添加为参数,如下所示:

<?php
$phpExecutable = 'C:/xampp/bin/php.exe' 
$path = 'C:/xampp/htdocs/user/execute.php';
exec($phpExecutable." ".$path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>

Should work. 应该管用。 I do not know where your php executable is located, so please adapt it to your location. 我不知道您的php可执行文件位于何处,因此请使其适应您的位置。

Happy coding. 快乐的编码。

First of all, as guys said in comments, you don't need exec() here at all. 首先,正如大家在评论中所说,这里根本不需要exec() You can just include that file. 您可以只include该文件。

Anyway, exec() function executes external program . 无论如何, exec()函数执行外部程序 A .php file is not a program, it's just a script that is executed by a program called php. .php文件不是程序,它只是由称为php的程序执行的脚本。

So you can run it like: 因此,您可以像这样运行它:

exec('php ' . $path, $output,$return);

php also can require you to give full path to its executable if it's not available globally. 如果无法全局使用php还可能要求您提供其可执行文件的完整路径。

http://php.net/manual/en/function.exec.php http://php.net/manual/en/function.exec.php

Adding 添加

exec("php execute.php > /dev/null &");

solved my problem..Thanks all 解决了我的问题。谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM