PHP exec运行文件

9

我已经尝试了3个小时,想让PHP运行一个简单的文件。我在本地主机(Windows 8)上使用Wamp服务器。

我已经尝试使用exec()函数:

 echo exec('whoami');

我收到了nt authority的响应。

也进行了以下测试:

if(function_exists('exec')) {
echo "exec is enabled";
}

那么它很可能有效?

我正在尝试运行一个名为tester.php的文件。

当我包含它时,它可以工作,当我需要它时也可以工作。我需要在后台执行它。当我刷新文件时,代码可以正常工作,没有任何错误,它可以将内容正常写入数据库。

但是当我尝试使用exec来执行它时,它就无法工作。

我尝试了以下方法:

       exec("php http://localhost/diplomski/program/defender/tester.php");
       exec("php-cli http://localhost/diplomski/program/defender/tester.php");
       exec("http://localhost/diplomski/program/defender/tester.php");

无法工作,尝试了以下方法:

        exec("php http://127.0.0.1/diplomski/program/defender/tester.php");
        exec("php-cli http://127.0.0.1/diplomski/program/defender/tester.php");
        exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php")

不起作用,还尝试了以下内容:
        exec("php tester.php");
        exec("php-cli tester.php");
        exec("tester.php");

也尝试过:

         $WshShell = new COM("WScript.Shell");
         $oExec = $WshShell->Run("D:\wamp\bin\php\php5.3.13\php-win.exe -f d:\wamp \www\diplomski\program\defender/tester.php", 0, false);

尝试过这个方法,但是刷新时没有效果:
        exec("php d:\wamp\www\diplomski\program\defender/tester.php");
        exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php");
        exec("d:\wamp\www\diplomski\program\defender/tester.php");

我正在拼命地思考,这是我第一次尝试使用exec(),我对它和命令并不熟悉。


PHP 是否被添加到环境变量? - litechip
那些带有URL的肯定不会起作用。你期望PHP解释器只使用URL做什么呢?它需要PHP代码才能运行。对于最后的尝试,具体是什么错误?PHP是否是该用户的路径环境变量的一部分? - David
3个回答

13

提供PHP可执行文件的完整路径和PHP脚本的完整路径。您可以将输出保存在$output中以查看脚本生成的内容:

exec("d:/path/to/php.exe d:/wamp/www/diplomski/program/defender/tester.php", $output);
print_r($output);

0
除了之前的回答,我想补充一点,如果你想在 PHP 中执行一个 PHP 文件,你可能会考虑使用 PHP 函数 include
include $path."tester.php"

我猜这比生成一个执行文件的新实例的新shell更有效率。但当然,“更好的选择”可能取决于上下文。


0

1) PHP的版本是多少?如果它比5.4.0旧,那么PHP可能处于安全模式。当安全模式启用时,您只能在safe_mode_exec_dir目录中执行文件。

2)请注意php.net上此函数的说明 注意:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

3) 所以你可以尝试这个如何让php脚本运行另一个php脚本,你可以试试看

 <?php
 $somearg = escapeshellarg('blah');
 exec("php file2.php $somearg > /dev/null &");

4) 你可以创建一个定时任务 如何在定时任务中运行PHP文件(Windows任务计划程序)

这是php5.3.13版本,还需要一个动态路径到exe。 - Avi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接