-2
root@ubuntu:/srv/www/web.domain.com/public_html# ls -l | grep 'aria2'
-rw-r--r--   1 nginx nginx      201 Aug 26 14:40 M010102.aria2
-rw-r--r--   1 nginx nginx     1313 Aug 26 14:41 [Heido] Hamasaki.aria2
-rw-r--r--   1 nginx nginx      125 Aug 26 14:41 [Riycou] K-Project [MP4 AAC 720p].aria2

I did the following

ls -l | grep 'aria2'

I wanna do this at PHP, i will probably

shell_exec("ls -l | grep 'aria2');

Problem is how do I record the return result into a php array

M010102.aria2 [Heido] Hamasaki.aria2 [Heido] Hamasaki.aria2

Thanks!!

Sal00m
  • 2,938
  • 3
  • 22
  • 33
user3504335
  • 177
  • 1
  • 13

3 Answers3

4

As per the official documentation for shell_exec, the output is the return value of the function:

<?php
  $output = shell_exec('ls -lart');
  echo "<pre>$output</pre>";
?>

RichardBernards' comment is correct. If you wish to have the output as an array, you can use the exec($command, &$output) function instead. Read the official documentation for further information.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
CarCzar
  • 140
  • 12
3

Why not just use PHP's glob()? It will return you an array of files:

$files = glob('*.aria2');

This is roughly the equivalent of the shell command ls *.aria2.

By the way, as a general piece of advice, parsing ls (by piping it to grep, for example) is frowned upon.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

You could use PHP commands to accomplish the same thing:

<?php
$directory = getcwd();
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
$result = preg_grep("/^.*\.aria2$/i",$scanned_directory);
foreach ($result as $value){
        echo $value,'<br>';
}
?>

More details:

Checking for file-extensions in PHP with Regular expressions

http://php.net/manual/en/function.preg-grep.php

http://php.net/manual/en/function.scandir.php

Community
  • 1
  • 1
bonafidegeek
  • 106
  • 3