Rineru Posted October 17, 2007 Share Posted October 17, 2007 Okay I have alot of FILE in one directory, maybe 100. What I need to do is have the index.php, grab the file name from each file and create a list of links in the index.php. so if I have file1.swf file2.swf file3.swf index.php in one directory. Whenever someone accesses the directory : http://www.example.com/example this will appear: File1 File2 File3 as links. Someone help me please. Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/ Share on other sites More sharing options...
BlueSkyIS Posted October 17, 2007 Share Posted October 17, 2007 from http://us2.php.net/readdir <?php if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { echo "<A href='$file'>$file</A><BR>"; } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-371720 Share on other sites More sharing options...
kenrbnsn Posted October 17, 2007 Share Posted October 17, 2007 You can also use the glob() function: <?php foreach(glob('*.swf') as $fn) echo '<a href="' . $fn . '">' . $fn . "</a><br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-371730 Share on other sites More sharing options...
Rineru Posted October 17, 2007 Author Share Posted October 17, 2007 Awesome, It works. Now a few little things. How can I get rid of the '.swf' from each item, and replace '_' with a ' ' (space)? Using the Glob function, which seems to work the best Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-371825 Share on other sites More sharing options...
marcus Posted October 17, 2007 Share Posted October 17, 2007 <?php foreach(glob('*.swf') as $fn){ $t = str_replace("_","", $fn); echo '<a href="' . $fn . '">' . ucfirst(substr($t, 0, -4)) . "</a> => $fn<br>\n"; } ?> outputs: Swf1 => swf1.swf Swf2 => swf2.swf Swf3 => swf_3.swf Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-371832 Share on other sites More sharing options...
kenrbnsn Posted October 17, 2007 Share Posted October 17, 2007 I would use the basename() function to remove the ".swf" from the file name: <?php <?php foreach(glob('*.swf') as $fn) echo '<a href="' . $fn . '">' . str_replace('_',' ',basename($fn,'.swf')) . "</a><br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-371845 Share on other sites More sharing options...
Rineru Posted October 18, 2007 Author Share Posted October 18, 2007 Thanks for all the help, everyone. Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-372026 Share on other sites More sharing options...
Rineru Posted October 18, 2007 Author Share Posted October 18, 2007 Okay, This topic was solved, but I want to know how to Alphabetize the output. Quote Link to comment https://forums.phpfreaks.com/topic/73680-including-filenames-in-a-list/#findComment-372105 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.