Scan Files Recursively

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AbsoluteBeginner
Forum Newbie
Posts: 2
Joined: Sat Jun 06, 2009 6:47 pm

Scan Files Recursively

Post by AbsoluteBeginner »

Hello!

I am very new to php and working on my first script to scan files and gathering information using shell_exec and the unix-command "du -sh".

I have just one problem...the script only lists files within /home/ and not in the subdirectories. How can i scan them?

Hints? Solutions? anybody?

Here the part of the script:
<?php
$files = glob('/home/*');

echo '<form action="" method="post">';
echo '<select name="file">';
sorry for bad english, im from europe
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Scan Files Recursively

Post by requinix »

As you guessed you have to do it recursively.

Code: Select all

function listfilesin($dir) {
    foreach (glob($dir . "/") as $file) { // list of all files in $dir
        echo $file, "<br>\n"; // print filename
        if (is_dir($file)) listfilesin($file); // look inside directories too
        // note that $file will have the directory name included already
    }
}
Run it, then try to modify it to fit your needs.
AbsoluteBeginner
Forum Newbie
Posts: 2
Joined: Sat Jun 06, 2009 6:47 pm

Re: Scan Files Recursively

Post by AbsoluteBeginner »

Hello,

thank you for your answer and the code, but it doesn't work.

here is full code, maybe something else wrong?

Code: Select all

<?php
$files = glob('/home/*');   
 
echo '<form action="" method="post">';
echo '<select name="file">';
 
foreach ($files as $file)
{
  if (!is_dir($file))
  {
    echo '<option>' . $file . '</option>';
  }
}
 
echo '</select>';
echo '<p><input type="submit" name="frm_filelist" /></p>';
echo '</form>';
 
if (isset($_POST['frm_filelist']))
{
  
  $file = $_POST['file'];
  $output = shell_exec('mediainfo ' . $file);
 
  echo '<form action="" method="post">';
  echo '<p>';
  echo '<textarea cols="60" rows="20">'
      . htmlentities($output) . '</textarea>';
  echo '</p>';
  echo '</form>';
}
?>
i tried everything, google etc... but i dont find the answer to the problem.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Scan Files Recursively

Post by requinix »

You forgot the recursion part.

It means you need a function. Like the one I posted. You only need to make one or two SMALL changes to it to get it to do what you want. Don't show directory names? Then don't print them. Want the file name wrapped in an <option>? Put one in.
Post Reply