PHP exec运行一个文件

时间:2013-11-13 18:33:20

标签: php exec

我正在尝试最后3个小时告诉PHP运行一个简单的文件。我在本地主机(Windows 8)中使用wamp服务器用于Windows

我尝试过使用exec()

 echo exec('whoami');

我得到了权威的回应。

还测试了:

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

所以它可能有用吗?

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

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

当我试图执行它时它不起作用。

我试过了:

       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()并且我对它或命令不太满意。

2 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:1)

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)所以你可以尝试这个How to make php script run another php script你可以试试这个

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

4)您可以创建计划任务How to run a PHP file in a scheduled task (Windows Task Scheduler)

相关问题