Page 1 of 1
Scan Files Recursively
Posted: Sat Jun 06, 2009 6:53 pm
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
Re: Scan Files Recursively
Posted: Sat Jun 06, 2009 7:19 pm
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.
Re: Scan Files Recursively
Posted: Sat Jun 06, 2009 7:55 pm
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.
Re: Scan Files Recursively
Posted: Sat Jun 06, 2009 8:46 pm
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.