2013-02-15 63 views
3

我有一個共享服務器Linux,基於此我正面臨一個奇怪的問題。我試圖通過PHP執行以下命令並且它運行正常;返回給我PHP安裝路徑/usr/bin/php在php中返回垃圾(HTTP響應)時執行exec('php -v')時出錯

exec('which php');// This runs so exec is not disabled 

但我嘗試與exec('php ...');執行任何命令失敗返回箱98到114元件中隨機地幾乎具有垃圾遍佈的陣列。我跑的命令的例子是...

exec('php -v'); 
exec('php -i'); 
exec('/usr/bin/php -v'); 

以上都沒有返回的東西明智。任何想法爲什麼任何命令運行的PHP不執行?

以下是exec()返回給我的數據數組的var_dump()

EDIT(後一些更RND)

我能夠執行

exec('php -h') 

和它reuturned我在可讀的格式下面的數組。

string(9) "php -help" 
string(47) "Usage: php [-q] [-h] [-s] [-v] [-i] [-f ]" 
string(27) "  php [args...]" 
string(36) " -a    Run interactively" 
string(69) " -b | Bind Path for external FASTCGI Server mode" 
string(57) " -C    Do not chdir to the script's directory" 
string(58) " -c | Look for php.ini file in this directory" 
string(47) " -n    No php.ini file will be used" 
string(56) " -d foo[=bar]  Define INI entry foo with value 'bar'" 
string(70) " -e    Generate extended information for debugger/profiler" 
string(46) " -f   Parse . Implies `-q'" 
string(28) " -h    This help" 
string(34) " -i    PHP information" 
string(43) " -l    Syntax check only (lint)" 
string(43) " -m    Show compiled in modules" 
string(60) " -q    Quiet-mode. Suppress HTTP Header output." 
string(60) " -s    Display colour syntax highlighted source." 
string(33) " -v    Version number" 
string(72) " -w    Display source with stripped comments and whitespace." 
string(46) " -z   Load Zend extension ." 
string(75) " -T  Measure execution time of script repeated times." 
This page was created in 0.055487155914307 seconds 

哦,順便說一句,我使用測試腳本如下...

<?php 
$mtime = microtime(); 
$mtime = explode(" ",$mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 


if(!empty($_GET['p']) && $_GET['p'] == true){ 

    //$command = 'php -help'; //this works 
    //$command = 'cat ' . getcwd() . '/dummy1.txt'; //this works echo's a simple text file 

    //$command = 'php -q ' . getcwd() . '/dummy1.txt'; //NOT WORKING 
    $command = 'php -m'; //NOT WORKING 

    echo '<div><pre>'; 
    var_dump($command); 
    echo '</pre></div>'; 

    exec($command, $output, $return); 
    //passthru($command,$output); 

    echo '<div><pre>'; 
    foreach($output as $key => $value){ 
     var_dump($output[$key]); 
    } 
    echo '</pre></div>'; 
} 


$mtime = microtime(); 
$mtime = explode(" ",$mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$endtime = $mtime; 
$totaltime = ($endtime - $starttime); 
echo "This page was created in ".$totaltime." seconds"; 
?> 

Pastebin.org Link

Image

+2

讓我理解正確..你正在執行php從... php? – 2013-02-15 09:14:44

+0

不要添加屏幕截圖..添加實際數據或將其放入pastebin – Baba 2013-02-15 09:18:57

+0

@AndrejsCainikovs Ya想通過這種方式運行獨立的進程。這是不可能的? – LoneWOLFs 2013-02-15 09:22:13

回答

3

它的PHP CGI從環境中讀取腳本名稱,從而導致調用腳本運行多次或有時無限次。

解決方案是簡單地使用命令php-cli而不是命令php。

替換了行$command = 'php -m';$command = 'php-cli -m';對你的代碼,一切都會正常工作。

+0

非常感謝它的工作答案。 – LoneWOLFs 2014-03-07 05:09:23

+0

這是'php-cli'一些linux只有二進制文件嗎?我無法在WAMP安裝中找到它 – LoneWOLFs 2014-06-13 06:44:56

+0

@LoneWOLFs for Windows'php -m'中的WAMP默認配置將毫無問題地完成您的工作。 – agaggi 2014-06-14 18:42:44

3

鑑於該數據似乎是一個HTTP響應,你有Content-Encoding: gzip那裏,我會使用gzdecode()字符串來解碼「垃圾」。

+1

提出了一個有趣的問題:爲什麼'exec('php')'輸出一個HTTP響應? – 2013-02-15 09:38:11

+0

我想這是來自名爲'dummy.php'的腳本的輸出。我猜pastebin包含'var_dump($ command,$ output)'的結果,所以腳本似乎迴應一個HTTP響應(2個標題字段,一個空行,然後是gzip內容)。 – cmbuckley 2013-02-15 09:48:10

+0

@cbuckley dummy.php文件只包含<?php echo「hello world」; ?>。那麼我應該怎麼做才能得到'Hello World'而不是HTTP Response。是的,它從'var_dump($ command,$ output)' – LoneWOLFs 2013-02-15 10:31:19