2015-04-16 82 views
2

我有兩個php文件。我們稱它們爲a.phpb.php獲取在後臺運行的php文件的輸出

當用戶訪問a.php時,我用exec()來執行b.phpb.php生成一個輸出。

我的問題是:如何在中完成過程完成時,如何將此輸出顯示在a.php或其他文件中?

這是我的代碼:論文

exec("C:\wamp\bin\php\php5.5.12\php.exe C:\wamp\www\b.php 2>otst.txt"); 
+1

exec()通常會阻止exec'd進程完成。這意味着在'b'完成之前你的'a'腳本已經在水中死了。但默認'exec'只返回程序輸出的最後一行。捕捉所有東西,你需要執行'exec(external_file,$ output,$ exit_value)'。 –

+0

OP應該精確的問題,特別是術語「顯示在」不適合php文件。 – Ratbert

+0

是的,我的壞。我會更新這個問題。 –

回答

0

使用一個2 PHP函數:

passthru - 執行外部程序並顯示原始輸出

system - 執行外部程序和顯示輸出

系統示例:

<?php 
echo '<pre>'; 

// Outputs all the result of shellcommand "ls", and returns 
// the last output line into $last_line. Stores the return value 
// of the shell command in $retval. 
$last_line = system('ls', $retval); 

// Printing additional info 
echo ' 
</pre> 
<hr />Last line of the output: ' . $last_line . ' 
<hr />Return value: ' . $retval; 
?> 

(來源http://php.net/manual/en/function.system.php

0

如果你不需要任何幻想,你可以使用backtick`)運營商,而不是exec()

$output = `php b.php`; 

注意,反引號對應主叫shell_exec,關於exec vs shell_exec的討論,請參閱此相關問題。