List and display files in a folder | Newbie | | Join Date: Jul 2008 Location: Santa Barbara
Posts: 8
| |
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 -
<?php
-
/*
-
about:
-
This snippet will list all the files in the directory of your
-
choice, truncate the file extension, capitalize the first letter and
-
put it all in a drop down menu. The script will not list subdirectories so
-
all you see are the files in your directory.
-
-
USAGE:
-
Change the $dirpath variable the directory you want to list.
-
-
-
*/
-
-
//Looks into the directory and returns the files, no subdirectories
-
-
//The path to the style directory
-
-
$dirpath = ".";
-
$dh = opendir($dirpath);
-
while (false !== ($file = readdir($dh))) {
-
-
//Don't list subdirectories
-
if (!is_dir("$dirpath/$file"))
-
{
-
-
//Truncate the file extension and capitalize the first letter
-
echo "<a href='$dirpath/$file'>". htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))). "<br />";"</a>";
-
-
}
-
}
-
closedir($dh);
-
?>
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | 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
| | | 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
| | | re: List and display files in a folder
Help!
I am not sure if I can figure this out on my own.
kkhan5000
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | re: List and display files in a folder | | Newbie | | Join Date: Jul 2008 Location: Santa Barbara
Posts: 8
| | | 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);
?>
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: List and display files in a folder Quote:
Originally Posted by kkhan5000 -
<?php
-
...
-
//Don't list subdirectories
-
if (!is_dir("$dirpath/$file")){
-
sort ($fileList); // sort the file list in the array
-
}
-
...
-
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
| | | 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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,947
| | | 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
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|