繁体   English   中英

PHP exec 运行文件

[英]PHP exec to run a file

我在过去 3 个小时里试图告诉 PHP 运行一个简单的文件。 我正在为本地主机中的 Windows 使用 wamp 服务器(Windows 8)

我试过exec()使用:

 echo exec('whoami');

我得到了回应nt权威。

还测试了:

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()并且我对它或命令不是很好。

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

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

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任务计划程序)

除了前面的答案,让我补充一点,如果你想从 PHP 中执行一个 PHP 文件,你可能需要考虑PHP 函数include代替:

include $path."tester.php"

我想这会(很多?)比产生一个新的 shell 来执行一个新的 PHP 实例来执行文件。 但当然,“更好的选择”的选择可能取决于上下文。

暂无
暂无

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

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