zipadee Posted May 19, 2008 Share Posted May 19, 2008 I want to have a page on my website that lists the files in a certain directory on my web server. How do I go about doing that? My php knowledge is limited thanks. Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/ Share on other sites More sharing options...
MadTechie Posted May 19, 2008 Share Posted May 19, 2008 heres a simple example from the manual opendir <?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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544712 Share on other sites More sharing options...
zipadee Posted May 19, 2008 Author Share Posted May 19, 2008 thanks! this is probably a dumb question but.... how do I get rid of the links to the directory and only link to the files within it? also, when I upload a file with space in its name, the link in the list won't work. how do I stop that happening? Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544720 Share on other sites More sharing options...
DarkWater Posted May 19, 2008 Share Posted May 19, 2008 urlencode() Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544721 Share on other sites More sharing options...
MadTechie Posted May 19, 2008 Share Posted May 19, 2008 try this (untested) (my boss is near ) <?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) { if($file != "." || $file != "..") { $file = urlencode($file); echo "filename: $file : <br>\n"; } } closedir($dh); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544722 Share on other sites More sharing options...
zipadee Posted May 19, 2008 Author Share Posted May 19, 2008 erm....it doesn't seem to work. but that could just be the way i'm implementing it :S i found this code in a tutorial: <?php $folder = "articles/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while ($file = readdir($handle)) { $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<a href=$folder$file>$file</a>"."<br />"; } ?> is that a legitimate way of doing it too? sorry, i'm not very good at this Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544739 Share on other sites More sharing options...
MadTechie Posted May 19, 2008 Share Posted May 19, 2008 try this <?php $folder = "articles/"; // Open a known directory, and proceed to read its contents if (is_dir($folder )) { if ($dh = opendir($folder)) { while (($file = readdir($dh)) !== false) { if($file != "." || $file != "..") { $ufile = urlencode($file); echo "<a href='".$folder."/".$ufile."'>$file</a>"."<br />"; } } closedir($dh); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544771 Share on other sites More sharing options...
947740 Posted May 19, 2008 Share Posted May 19, 2008 You could use scandir(). *Not Tested <?php $folder = "something/" $dir = $_SERVER['DOCUMENT_ROOT']."/".$folder; $files = scandir($dir); foreach($files as $number => $filename) { if(filetype($filename) == "dir") { } else { echo "<a href='".$dir.$filename."'>$filename</a><br />"; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/106292-how-to-list-files-on-server/#findComment-544943 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.