在返回垃圾(HTTP响应)的php中执行exec(';php-v';)时出错
我有一个共享服务器Linux,基于它我面临一个奇怪的问题。我试图通过PHP执行以下命令,它运行正常;返回PHP安装路径在返回垃圾(HTTP响应)的php中执行exec(';php-v';)时出错,php,linux,exec,shared-hosting,Php,Linux,Exec,Shared Hosting,我有一个共享服务器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');
/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运行的任何命令都没有执行吗
下面是exec()
返回给我的数据数组的var\u dump()
编辑(在更多的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";
?>
假设数据似乎是HTTP响应,并且其中包含内容编码:gzip
,我将使用字符串对“垃圾”进行解码。它的PHP CGI从环境中读取脚本名称,从而导致调用脚本运行多次或有时无限次
解决方案只是使用命令php cli而不是命令php
替换行$command='php-m'代码>与$command='php cli-m'代码>在您的代码上,一切正常。请让我正确理解。。您正在从执行php。。。php?不要添加屏幕截图。。添加实际数据或将其放入pastebin@AndrejsCainikovsYa希望通过这种方式运行独立的流程。这是不可能的?@Baba添加了pastebin链接:)为什么不使用phpversion()
,这带来了一个有趣的问题:exec('php')
为什么输出HTTP响应?我想这是名为dummy.php
的脚本的输出。我猜pastebin包含var_dump($command,$output)
的结果,因此脚本似乎响应HTTP响应(两个头字段,一个空行,然后是gzip内容)。@cbuckley dummy.php文件只包含一个。那么我应该怎么做才能准确地得到“Hello World”而不是HTTP响应呢。是的,这是从var\u dump($command,$output)
@cbuckley添加了一些新发现,你能研究一下吗?@LoneWOLFs如果是这样的话,我唯一能想到的就是。这是php cli
某种linux专用二进制文件吗?我在我的WAMP里找不到它installation@LoneWOLFs对于WAMP,windows中的默认配置“php-m”将毫无问题地完成您的工作。