GeorgeC
12-16-2005, 08:29 AM
Hi:
I wish to use opendir() to open the current directory the PHP script is in to display all files in it. I tried:
$handle=opendir(".")
which seems to work. Is this the best way though? Any security concerns I should be worried about? On php.net they seem to contruct the entire path to the current directory instead of using "."
Thanks!
ZiDev
12-17-2005, 08:50 PM
If you know the full path, you can use it, but often you don't (i.e. if you're distributing the script).
Niether is less secure than the other because there is no user input.
UrlGuy
12-18-2005, 01:00 PM
maybe get path with getcwd for full path? hm dunno..
Here's what I'm using to list directory content if it can help:
<?php
$self = scandir(getcwd());
for($i=0;$i<count($self);$i++) {
echo "<a href=$self[$i]>".$self[$i]."</a><br>";
}
?>
mdnava
12-20-2005, 11:19 PM
That would work just fine...
I have a quick dir listing script in case you may find it useful:
<?php
$dir_path = '.';
$dir_list = array();
if ($handle = opendir($dir_path)) {
while ($files = readdir($handle)) {
if (preg_match("/^\./",$files)==0) echo $files.'<br>'; //$dir_list[] = $getn;
}
closedir($handle);
}
?>
mdnava
12-20-2005, 11:27 PM
Hi again, here's a mod to sort files in alphabetical order:
<?php
$dir_path = '.';
$dir_list = array();
if ($handle = opendir($dir_path)) {
while ($files = readdir($handle)) {
if (preg_match("/^\./",$files)==0) $dir_list[] = $files; //echo $files.'<br>';
}
closedir($handle);
asort($dir_list);
echo '<h3>Directory Listing</h3><ul>';
foreach ($dir_list as $value) {
echo '<li>'.$value.'</li>';
}
echo '</ul>';
}
?>