PHP 可执行文件来运行文件


PHP exec to run a file

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

我尝试过exec()使用:

 echo exec('whoami');

我得到了回应的权威。

还经过以下测试:

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

所以它可能有效吗?

我正在尝试运行一个名为测试器的文件.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更有效,该shell执行该文件的新PHP实例。但当然,"更好的选择"的选择可能取决于上下文。