Jump to content


Photo

list files


  • Please log in to reply
4 replies to this topic

#1 GeorgeMoney

GeorgeMoney
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 10 September 2006 - 02:45 AM

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!
--GeorgeMoney

#2 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 10 September 2006 - 03:19 AM

straight from the source:

http://us3.php.net/m...ion.opendir.php


<?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);
   }
}
?> 

Jeff

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 04:07 AM

You can also use the glob() function (http://www.php.net/glob)

Ken

#4 °°Ben³

°°Ben³
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationGermany / Remagen

Posted 10 September 2006 - 01:14 PM

Try this if you use a version >= PHP 5.

<?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()
    );
}
?>

Regards, Ben.
desktop and web development ;)
---
English is not my native language. Please excuse if I made some errors in expression.

#5 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 01:31 PM

<?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>";
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users