php exec输出被裁剪 [英] php exec output being trimmed

查看:67
本文介绍了php exec输出被裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php中使用exec再次遇到另一个麻烦
我的操作系统是suse linux,我正在使用php 5.1.2

I have again another trouble with using exec in php
my os is suse linux and I am using php 5.1.2

使用exec()时,输出会被修整

Somehow my output is being trimmed when i use exec()

在Linux中

~ -> ps -ef | grep java
root      3548     1  0 Aug05 ?        00:00:01 /usr/java/jdk1.5.0_13//bin/java -server -Djava.awt.headless=true -Xms512m -Xmx512m -XX:NewSize=224m -XX:MaxNewSize=256m -XX:SurvivorRatio=8 -XX:+UseParallelGC -jar /jfe-server.jar start
psinl    14811     1  0 09:12 ?        00:00:01 /usr/java/jdk1.5.0_13//bin/java -server -Djava.awt.headless=true -Xms512m -Xmx512m -XX:NewSize=224m -XX:MaxNewSize=256m -XX:SurvivorRatio=8 -XX:+UseParallelGC -jar jfe-server.jar start
psinl    18164 18080  0 16:20 pts/1    00:00:00 grep java

但是当通过

<div>Checking whether JFEServer has been started</div>
<div><pre><?php exec('ps -ef | grep java',$output,$result);
print_r($output); ?></pre>
</div>
</br>

我在网络上的输出

Checking whether JFEServer has been started

Array
(
    [0] => root      3548     1  0 Aug05 ?        00:00:01 /usr/java/jdk1.5.0_13//bin/java
    [1] => psinl    14811     1  0 09:13 ?        00:00:01 /usr/java/jdk1.5.0_13//bin/java
    [2] => psinl    18069 14271  0 16:20 ?        00:00:00 sh -c ps -ef | grep java
    [3] => psinl    18071 18069  0 16:20 ?        00:00:00 grep java
)

为什么即使我不想要php,php也会自动修剪我的输出?

Why is that php has automatically trimmed off my output even I didnt want it to?

推荐答案

这是因为PHP不能为您分配与exec一起使用的足够大的缓冲区.最好的选择是在两者之间添加一个步骤:在exec()调用

This is because PHP just doesn't can't allocate a large enough buffer for you to use with exec. Your best bet is to add a step in between: pipe the output to a temporary file in your exec() call

示例:exec('ps -ef | grep java > /tmp/mytmpfilename.txt')

...然后通过调用file_get_contents()

...then dump that out to the screen with a call to file_get_contents()

示例:var_dump(file_get_contents('/tmp/mytmpfilename.txt'));

或者,如果有很多数据要输出,例如价值几千行,则可以使用file().

edit: Alternatively, you can use file() if there is a LOT data to output, like several thousand lines worth.

这篇关于php exec输出被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆