Connecting Tech Pros Worldwide Forums | Help | Site Map

List and display files in a folder

Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#1: Jul 3 '08
Hello,

I am new to PHP and have put together the following code just by copying other examples from the web. Now I am stuck and would like to sort and display files alphabetically, and also filter what file type to display or not display. Thank you for your help.

-kkhan5000
Expand|Select|Wrap|Line Numbers
  1. <?php  
  2. /*
  3. about:
  4. This snippet will list all the files in the directory of your
  5. choice, truncate the file extension, capitalize the first letter and
  6. put it all in a drop down menu. The script will not list subdirectories so 
  7. all you see are the files in your directory.
  8.  
  9. USAGE:
  10. Change the $dirpath variable the directory you want to list.
  11.  
  12.  
  13. */
  14.  
  15. //Looks into the directory and returns the files, no subdirectories
  16.  
  17. //The path to the style directory
  18.  
  19. $dirpath = ".";
  20. $dh = opendir($dirpath);
  21. while (false !== ($file = readdir($dh))) {
  22.  
  23. //Don't list subdirectories
  24. if (!is_dir("$dirpath/$file"))
  25. {
  26.  
  27. //Truncate the file extension and capitalize the first letter
  28. echo "<a href='$dirpath/$file'>". htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))). "<br />";"</a>";
  29.  
  30. }
  31. }  
  32. closedir($dh); 
  33. ?>

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#2: Jul 3 '08

re: List and display files in a folder


To sort them alphabetically, you'd have to create an array that holds the file name and location. Then sort() the array.
Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#3: Jul 4 '08

re: List and display files in a folder


Thank you markusn00b, I will start learning about arrays.
-kkhan5000
Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#4: Jul 4 '08

re: List and display files in a folder


Help!

I am not sure if I can figure this out on my own.

kkhan5000
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#5: Jul 4 '08

re: List and display files in a folder


Read about arrays.
Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#6: Jul 4 '08

re: List and display files in a folder


Thank you for the help with arrays. It is working now as far as the sorting is concerned, but it is now showing the sub directories. How do I filter out folders and file types. Here is what I have now:

<?php
$dirpath = ".";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
$fileList[] = trim($file);

//Don't list subdirectories
if (!is_dir("$dirpath/$file")){
sort ($fileList); // sort the file list in the array
}
}
reset ($fileList); // go back to the top of the array
while (list ($key, $file) = each ($fileList))
{
//Truncate the file extension and capitalize the first letter
echo "<a href='$dirpath/$file'>". htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))). "<br />";"</a>";


}
closedir($dh);
?>
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#7: Jul 5 '08

re: List and display files in a folder


Quote:

Originally Posted by kkhan5000

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ...
  3. //Don't list subdirectories
  4. if (!is_dir("$dirpath/$file")){
  5.      sort ($fileList); // sort the file list in the array
  6. }
  7. ...
  8.  

I'm no expert, but maybe where your code says "Don't list subdirectories" has something to do with it.

Check out this code:

http://codingforums.com/showthread.php?t=71882

If you have any questions implementing that, let us know!


-Dan
Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#8: Jul 5 '08

re: List and display files in a folder


Thank you Dan,

I fixed the subdirectory problem by re-organizing the code. Now the only last thing left is to display only a certain type of files instead of everything the folder.

Thank you,

-kk
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#9: Jul 5 '08

re: List and display files in a folder


Quote:

Originally Posted by kkhan5000

Thank you Dan,

I fixed the subdirectory problem by re-organizing the code. Now the only last thing left is to display only a certain type of files instead of everything the folder.

Thank you,

-kk

You could get the file extension and then check to see whether it's in an array of allowed extensions.
Newbie
 
Join Date: Jul 2008
Location: Santa Barbara
Posts: 8
#10: Jul 6 '08

re: List and display files in a folder


Thank you markusn00b ,

I think I got it to work somewhat by using "!is_file". Well, I have something now what I needed and I can use. However, during the process of learning php, my requirements have considerabley changed. I am not a coder, period, and would like to pay if needed to get the following done.

I would like to use all lower case file names, which are hyphenated or underscored like: "nice_day.php or nice-day.htm". And, I would like to display them as "Nice Day" in a web page or navigation bar. The goal is to drop an php/html document in a folder which dynamically shows up as a link in a navigation area of the web page.

Any advice would be greatly appreciated.

-kk
Reply