0

我有一个 php 代码,它将 ssh 到远程机器并执行一个 shell 脚本来获取文件夹列表。远程机器在 shell 脚本的指定路径中包含 300 多个文件夹。shell 脚本执行良好并返回所有文件夹的列表。但是当我在 php 中检索此输出时,我只得到大约 150、200 个文件夹.

这是我的php代码,

    <?php
    if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
    if(!($con = ssh2_connect("ip.add.re.ss", "port")))
    {
        echo "fail: unable to establish connection";
        }
    else
    {
        if(!ssh2_auth_password($con, "username", "password"))
    {
        echo "fail: unable to authenticate";
    }
    else
        {
        $stream = ssh2_exec($con, "/usr/local/listdomain/listproject.sh");
        stream_set_blocking($stream, true);
        $item = fread($stream,4096);
        $items = explode(" ", $item);
        print_r($items);
    }
        }
    ?>

这是我的 shell 脚本。

#!/bin/bash
var=$(ls /home);
echo $var;

这里的php有什么问题。像这里一样动态获取数据时,php 中的数组大小是否有任何限制。请告知,因为我是 PHP 的初学者。

谢谢。

4

2 回答 2

0

您只从流中读取一个 4096 个字符的块。如果您的文件夹列表比这长,您将失去其余的。你需要这样的东西:

    stream_set_blocking($stream, true);
    $item = "";
    // continue reading while there's more data
    while ($input = fread($stream,4096)) {
       $item .= $input;
    }

    $items = explode(" ", $item);
    print_r($items);
于 2013-07-02T05:01:10.003 回答
0

您要求fread()仅读取 4096 个字节。在 的文档的示例部分中fread(),建议stream_get_contents()用于将文件句柄读取到末尾。否则,您必须使用循环并继续读取数据,直到feof($stream)返回FALSE

于 2013-07-02T05:03:38.270 回答