【问题标题】:How do you pass the stdout to a linux terminal as an argument in a php script?如何将标准输出作为 php 脚本中的参数传递给 linux 终端?
【发布时间】:2012-01-13 06:39:22
【问题描述】:

有一个名为 getID3(用 php 编写)的程序,它可以从您的音乐中复制所有 id3 标签并以文本形式打印出来:http://getid3.sourceforge.net/

有一个功能需要你歌曲的路径,我想写一些代码来自动化整个过程,这样我就可以只提供父目录的路径,所有子目录中的所有歌曲都可以有为我打印出他们的 id3 信息。

我想通过这个 BASH 命令获取歌曲的所有路径:

find /myMusic -type f

并将 BASH 命令中的每个路径加载为 PHP 数组中的一个元素(比如说$song_information),其中数组是

find /myMusic -type f |wc -l

元素长。

我被卡住了,因为我不知道如何将 BASH 终端的输出获取到 php.ini 中的数组。我的第一想法是将 bash 命令的输出通过管道传输到 .xml 文件,然后使用 php.xml 解析器将每个路径加载到数组中。

这种思路是好方法还是有更好的方法?

我可用的工具是 linux 和 php。我正在使用 ubuntu 10.04。

【问题讨论】:

    标签: php linux bash


    【解决方案1】:
    <?php
    
    $list = array();
    
    exec ( 'find /myMusic -type f', $list );
    
    ?>
    

    【讨论】:

      【解决方案2】:

      "Input/output streams"

      fgets()

      如果$a 是一个数组,那么分配给$a[] 将附加一个元素。

      【讨论】:

        【解决方案3】:

        您的 PHP 可能如下所示:

        #!/usr/bin/php
        <?
        $stdin = fopen('php://stdin', 'r');
        while (($buffer = fgets($stdin, 4096)) !== false) {
                $lines[] = $buffer;
        }
        fclose($stdin);
        
        print_r($lines);
        
        ?>
        

        然后通过

        将结果传递给它
        find /myMusic -type f | ./yourscript.php
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-23
          相关资源
          最近更新 更多