If I want to list a directory's contents as hyperlinks I can go about it in two very simple ways. Both methods allow me to display files based on their extension, and with an added 'is_dir' statement I may omit directories if I want.
The first way is to use 'glob' like this:
Code: Select all
<?php
foreach (glob('./' . '*', 0) as $file) {
echo '<a href="' . $file . '">' . basename($file) . '</a><br>' . PHP_EOL;
}
?>
This way is short and simple.
The second way is to use 'exec' like this:
Code: Select all
<?php
$files = array();
exec('ls ./', $files);
foreach ($files as $file) {
echo '<a href="./' . $file . '">' . $file . '</a><br>' . PHP_EOL;
}
?>
I like this method better, as reading directories recursively is much easier. And I get all the power of ls, so I can list modification times, permissions, user, group, file size, and all that with the one 'exec' line. However, if I'm not mistaken, 'exec' invokes the operating system's command-line and returns its output. So it seems that this method would be slower or more prone to security issues. (I already know it doesn't work on Windows servers, and I really don't care about that.)
Is there any significant reason not to use 'exec' instead of 'glob' for stuff like this?