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! Link to comment https://forums.phpfreaks.com/topic/20263-list-files/ 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 Link to comment https://forums.phpfreaks.com/topic/20263-list-files/#findComment-89169 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 Link to comment https://forums.phpfreaks.com/topic/20263-list-files/#findComment-89178 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. Link to comment https://forums.phpfreaks.com/topic/20263-list-files/#findComment-89326 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 Link to comment https://forums.phpfreaks.com/topic/20263-list-files/#findComment-89329 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.