4

我有一个共享服务器 Linux,基于它我面临一个奇怪的问题。我正在尝试通过 PHP 执行以下命令,它运行正常;将 PHP 安装路径返回给我/usr/bin/php

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 运行的任何命令都没有执行吗?

下面是返回给我var_dump()的数据数组。exec()

编辑(经过更多 RND)

我能够执行

exec('php -h')

它以可读的格式返回给我以下数组。

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 链接

图片

4

2 回答 2

4

它的 PHP CGI 从环境中读取脚本名称,从而导致调用脚本运行多次或有时无限次。

解决方案是简单地使用命令 php-cli 而不是命令 php。

在您的代码上替换为行$command = 'php -m';$command = 'php-cli -m';一切都会正常工作。

于 2014-03-06T19:14:23.350 回答
3

鉴于数据似乎是 HTTP 响应,并且您Content-Encoding: gzip在其中,我将使用gzdecode()字符串来解码“垃圾”。

于 2013-02-15T09:29:15.120 回答