简体   繁体   中英

How do you pass the stdout to a linux terminal as an argument in a php script?

There's a program called getID3 (written in php) that copies all the id3 tags from your music and prints it out in text form: http://getid3.sourceforge.net/

There's one function that requires the path of your song and I would like to write some code to automate the whole process so that I can provide just the path of the parent directory, and all the songs in all the sub directories can have their id3 information printed out for me.

I want to get all the paths of the songs by using this BASH command:

find /myMusic -type f

And load each path from the BASH command as an element in a PHP array (let's say $song_information ) where the array is

find /myMusic -type f |wc -l

elements long.

I'm stuck because I don't know how to get the output from the BASH terminal to an array in php. My first line of thinking was pipe the output of the bash command to an .xml file and then use a php.xml parser to load each path into an array.

Is this line of thinking a good approach or is there a better way?

The tools I have available are linux and php. I'm using ubuntu 10.04.

<?php

$list = array();

exec ( 'find /myMusic -type f', $list );

?>

"Input/output streams"

fgets()

If $a is an array then assigning to $a[] will append an element.

Your PHP can look like this:

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

print_r($lines);

?>

and you pipe the results to it via

find /myMusic -type f | ./yourscript.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM