簡體   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