Computing.Net > Forums > Programming > PHP Customising Directory listing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP Customising Directory listing

Reply to Message Icon

Name: Shaun
Date: October 20, 2003 at 12:28:35 Pacific
OS: Windows XP Pro
CPU/Ram: 1800XP / DDR
Comment:

Hi, I am having a problem. I can find snippets of code on the web but nothing designed to do what I want.

I have a directory which I share on my IIS server, it is just a link to a directory with directory browsing enabled.

I would like to customise this page with a background / showing just mp3's and make all the mp3's clickable etc etc and I don't know how to program this using PHP (I'm very new to it)

I have tried opendir() but I think this is just for internal calls.

Any help would be much appreciated,

Thanks a lot

Shaun



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: October 20, 2003 at 12:49:07 Pacific
Reply:

Below is a script that I wrote in PHP to give me a directory readout of all my dirs and files and hyperlinks them for access in a web browser. Perhaps you will find it useful for what you are trying to do.

--------
<?php
$imgDir = opendir("YOUR-MP3-DIRECTORY");
while($imgName = readdir($imgDir))
{
if (($imgName != ".") || ($imgName != ".."))
print("$imgName
\n");
}
?>
--------

I have a few questions for you now ;)
1) Why choose IIS over Apache?
2) Have any good music??

At any rate, this script should get you what you want, adding a background image to the site is fairly trivial. Post back here if you have problems.

IR


0

Response Number 2
Name: Infinite Recursion
Date: October 20, 2003 at 12:52:34 Pacific
Reply:

Thought I could just throw code out and have it work in the forum, I guess I need to use the coder facility next time as I lost some code. The print line should read as follows:

print("[LT]A HREF=\"$imgName\"[GT]$imgName[LT]/A[GT][LT]BR[GT]\n");

The [LT] is a less than symbol.
The [GT] is a greater than symbol.

IR


0

Response Number 3
Name: Shaun
Date: October 20, 2003 at 13:22:14 Pacific
Reply:

Hi,

Fantastic - That's nearly done the trick thanks,

I have a couple more questions:

1) How can I exclusively specify what file extentions to show

2) How can I tell it to show just folders and/or files in a specific folder

3) Would I need to place this in each folder or can I use it to browse through folders/subfolders and back again.

In answer to your questions :

1) Simply because we run IIS at my place of work and I would do far better to learn how IIS works for my job. I'm sure apache is better - I have never used it before.

2) Please feel free to take a look:

http://80.6.32.33:8080
Username: computing
Password: help

I am using your program here http://80.6.32.33:8080/music/song lists/athlete/listing.php

and here

http://80.6.32.33:8080/music/song lists/listing.php

You see - I have many folders and files and I would like to be able to use your code after a successful login to browse through the folders and files and back again...without pasting the listing.php in every folder.

Thanks for your help, much appreciated,

Shaun


0

Response Number 4
Name: zeroguy
Date: October 20, 2003 at 17:59:38 Pacific
Reply:

1) To only show certain exensions, change the if statement to:


if ((($imgName != ".") || ($imgName != "..")) && (strcmp(substr($imgName, -4), ".mp3")))

to only show '.mp3's. Just change the .mp3 to something else for other extensions, but if the extension is 4 characters instead of three, you have to change the -4 to a -5. I think that should work.

2) This is a little trickier. The only way I can think of is to exec() the command `ls -l`, and look for directory entries. I'll provide more details if others think this is a good idea (IR?)

3) Not necessarily. You could have a variable that gets passed to the script when you click on a directory. Like if you changed the first $imgName in IR's print statement in his second post to $_SERVER['PHP_SELF'] . "?dir=$imgName" or something. But I'm not sure if $_SERVER is used in IIS, as I usually use Apache. But this might be getting a little advanced, so put it in every directory if you want.

And by the way, the reason that the '.'s and '..'s are showing up is that IR violated one of the most important rules with strings (haha), in that he used != to compare them! bah. So actually that if statement should be:

if ((strcmp($imgName, ".") != 0) || (strcmp($imgName, ".." != 0)))

unless I'm missing something. Remember, I'm not familiar with IIS, so I'm just speculating. Have fun!

-zeroguy


0

Response Number 5
Name: Infinite Recursion
Date: October 20, 2003 at 19:34:07 Pacific
Reply:

1) Zeroguy answered that question for you... just be careful to accomodate the file extensions accordingly.

2) You can have a comparison for each element of the array using the IsAFile and/or IsADirectory function. I came across these functions before, they are fairly
obscure, maybe they are in a wrapper class. But they will work when you find them...

3) Place the directory script I wrote for you in your main directory, I suppose on IIS that would be your Inetpub by default, on Apache it is the htdocs. This way, you can browse every directory that has read / listing privilege. Just put my script where ever you want your "root" to be.

---

1) Smart man, that's the reason I wanted to run Apache.

2) Looks good. Glad the my script came in handy for you, I knew it would, it was handy for me ;). I haven't had a chance to review your music yet... but noticed some of my old favorites. I'll have to check it out, when this damn program stops executing, its been running since 730am, its now 9:21pm.

Again, you don't need the listing.php in every folder, just place it in your main public HTML directory (inetpub for IIS, htdocs for Apache) and it will scan and include every directory you have that has listing priveleges... it automatically filters the ones that are locked down.

----

Zeroguy:

You are right about the . and .. showing up and the reasons that they do... However, perhaps in my instance of this script, I overloaded the != operator or have redefined the string class. Both possible... But, no... I didn't do that. Honest, mistake...
wasn't able to test it off hand. This script was originally part of a larger project where the directory level options were needed, I suppose that's why I never caught it... just wasn't a problem.

Glad it worked out, looks good :)

IR


0

Related Posts

See More



Response Number 6
Name: Berdon Magnus
Date: October 20, 2003 at 21:48:44 Pacific
Reply:

You might just have the link go back to the main listing.php but with a variable of the group directory name. THen you would only need one of the files.


0

Response Number 7
Name: Shaun
Date: October 21, 2003 at 06:17:16 Pacific
Reply:

Thanks for all your help,

The if statement to remove all files that are not mp3's works in reverse. It allows all files that are not mp3!! :) How do I change the statement to allow all mp3's??

ORIGINAL IF STATEMENT:

if ((($imgName != ".") || ($imgName != "..")) && (strcmp(substr($imgName, -4), ".mp3")))

Thanks

Shaun


0

Response Number 8
Name: Infinite Recursion
Date: October 21, 2003 at 06:57:23 Pacific
Reply:

I am sure someone has a better way...
All I did was make the addition of the not operator (!). Try this out and see if it works...

if ((($imgName != ".") || ($imgName != "..")) && (!(strcmp(substr($imgName, -4), ".mp3"))))

IR


0

Response Number 9
Name: zeroguy
Date: October 21, 2003 at 12:36:52 Pacific
Reply:

Terribly sorry, i meant strcmp(substr($imgName, -4), ".mp3") == 0 , I just forgot the == 0 at the end, but IR's solution should work just as well.


0

Response Number 10
Name: Shaun
Date: October 22, 2003 at 02:25:27 Pacific
Reply:

Hi,

Thank you all for your help!

I don't know any PHP and so your help has been invaluable. My directory listing's look so much better.

If you want to download any songs, please do:

http://80.6.32.33:8080

Username: computing
Password: help

Thanks again,

Shaun


0

Response Number 11
Name: Infinite Recursion
Date: October 22, 2003 at 06:29:20 Pacific
Reply:

You're welcome. Glad to see you have it working...

IR


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: PHP Customising Directory listing

PHP Directory Listing www.computing.net/answers/programming/php-directory-listing/8345.html

htaccess directory listings help www.computing.net/answers/programming/htaccess-directory-listings-help/15646.html

javascript equiv of index.php? www.computing.net/answers/programming/javascript-equiv-of-indexphp/12465.html