473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

List and display files in a folder

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. ?>
Jul 3 '08 #1
9 4303
Markus
6,050 Expert 4TB
To sort them alphabetically, you'd have to create an array that holds the file name and location. Then sort() the array.
Jul 3 '08 #2
Thank you markusn00b, I will start learning about arrays.
-kkhan5000
Jul 4 '08 #3
Help!

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

kkhan5000
Jul 4 '08 #4
Markus
6,050 Expert 4TB
Read about arrays.
Jul 4 '08 #5
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);
?>
Jul 4 '08 #6
dlite922
1,584 Expert 1GB
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
Jul 5 '08 #7
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
Jul 5 '08 #8
Markus
6,050 Expert 4TB
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.
Jul 5 '08 #9
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
Jul 6 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ken | last post by:
I currently have a set of documents in a directory that i need to list in a html table. Is there any way to generate the table with the documents listed instead of having to update the table...
2
by: Jean-Paul Lauque | last post by:
Hello, I'm beginning in the ASP world... I would like to sort (descending) list of files in directory. Parameter is directory url. How I can to do that with ASP not ASP.NET.
4
by: Jake | last post by:
Im using the following code to display the contents of a directory: <% Set objFso = CreateObject("Scripting.FileSystemObject") Set objFiles = objFso.GetFolder(Server.MapPath(".")) Set fileList...
1
by: John | last post by:
Hi, we have the following problem: in our application v1.0 we have static html pages (+ some javascript). With a CMS (a Content Management System) some "mortal" people can add or expand the...
5
by: nospam | last post by:
Hi there, I'm using VBScript to display a list of the ten most recently updated pages on my web site. Right now, the script lists the filenames and the date modified in a given directory. ...
1
by: shank | last post by:
I borrowed the below code from http://www.brainjar.com/asp/dirlist/ and cannot get it to read my folder files. I don't get any errors or any output to screen. I have the below folder permissions...
4
by: maglev_now | last post by:
I'm using .net 1.1 trying to get a list of files in folder on the server. The user would select the file they want to download from a DropDownList. Can someone tell me how this should be done? I...
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
6
by: kimiraikkonen | last post by:
Hello, I have a listbox and folder browser control. I need to display all .mp3 files' pathes into listbox. It was Ok with openfiledialog but how can list all .mp3 extension- having files into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.