Jump to content

Archived

This topic is now archived and is closed to further replies.

GeorgeMoney

list files

Recommended Posts

Is there a way to list all the files in a directory just using a php script, not any server-configuration?

Or would I have to do something like a loop and go through the loop checking if random filenames exist? if so could someone help me to do that?

Thanks in advance!

Share this post


Link to post
Share on other sites
straight from the source:

[url=http://us3.php.net/manual/en/function.opendir.php]http://us3.php.net/manual/en/function.opendir.php[/url]


[code]
<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
          echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
      }
      closedir($dh);
  }
}
?>
[/code]

Jeff

Share this post


Link to post
Share on other sites
You can also use the glob() function (http://www.php.net/glob)

Ken

Share this post


Link to post
Share on other sites
Try this if you use a version >= PHP 5.

[code=php:0]<?php
$dir = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('.' . DIRECTORY_SEPARATOR)
);

foreach ($dir as $file) {
    printf(
        '%s %s is a %s' . PHP_EOL,
        str_repeat(' ', $dir->getDepth()),
        $file,
        $file->getType()
    );
}
?>[/code]

Regards, Ben.

Share this post


Link to post
Share on other sites
[code]
<?php
$dir_name = "uploads/";
$dir = opendir($dir_name);

$file_list = "<ol>";
while ($file_name = readdir($dir)){

if (($file_name != ".") && ($file_name != "..")) {
$file_list .= "<li><a href=\"uploads/".$file_name."\">".$file_name;
echo "</a>";
}
}

$file_list .= "</ol>";
closedir($dir);
echo "<div align=\"left\">";
echo "The files currently uploaded are as follows:<br />".$file_list;
echo "</div>";
?>[/code]

that also makes each item in the list a link to that item

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.