GeorgeMoney Posted September 10, 2006 Share Posted September 10, 2006 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! Quote Link to comment Share on other sites More sharing options...
jefkin Posted September 10, 2006 Share Posted September 10, 2006 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 contentsif (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; } closedir($dh); }}?> [/code]Jeff Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 10, 2006 Share Posted September 10, 2006 You can also use the glob() function (http://www.php.net/glob)Ken Quote Link to comment Share on other sites More sharing options...
°°Ben³ Posted September 10, 2006 Share Posted September 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
perezf Posted September 10, 2006 Share Posted September 10, 2006 [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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.