Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Dr. Nick
Hey all,
I was looking around and saw this post about listing mp3 files. I thought it was a cool idea, so I tried on my XP system running IIS. The problem I'm having is that it does not read subdirectories.
I thought that since I'm on Windows and pretty much everything has an extension, I could just see if the file has a '.' in it and if not, then it must be a directory.
I'd think there was a better way to do this, maybe a built in function that automatically checks subdirectories.
Here's the code I'm using, almost verbatim from the earlier post. What I get is any files with the .XXX extension, but it does not check subdirectories.
<?
$dir = opendir("DIR");
while($fileName = readdir($dir))
if (!(strcmp(substr($fileName, -4), ".XXX")))
print("[a href=\"DIR\\$fileName\"]$fileName[/a][br]\n");?>
What do you think?

Dr. Nick,
I ran the PHP code sent to Shaun in the post that you linked, with slight modifications, on Windows XP Pro, Windows 2000 Pro and Gentoo Linux without difficulty.
So I am unaware of the difficult that you are having. Does the script list sub directories for you? Does the script let you access sub directories? The reason I ask, is because every instance of my script has been ran under the Apache web server, and not MS IIS. Make sure you have your directory browsing enabled in your configuration file for IIS and ensure that all configuration settings are set properly to ensure access to sub directories.
Not all files have a . within the name, some files do not have extensions on a system level. You personal data files etc, I am sure will be different.
Basically, ensure your IIS configuration allows directory browsing and directory access. If for some reason you IIS configuration is set properly and you still have problems, try and tinker with the functions below:
is_dir($file);
is_executable($file);
is_file($file);
is_link($file);
is_readable($file);
is_writable($file);If you still have problems, post back and I'll look into it further.
IR

Also, your DIR will need to be an actual directory path... for instance:
$imgDir = opendir("C:\mydata");
You may also want to try removing the ! in the strcmp.
If these suggestions, here and above, make no sense... I am unclear on what your problem is.
1) Does the script list sub directories for you?
2) Does the script let you access sub directories?Just trying to determine if its a configuration error of IIS or PHP, or script error.
IR

IR
Thanks for your replies. When I run the code in my first post, I get all the files with the .xxx extension, and nothing else. If I remove the IF statement, I get all the files, folders, ., and .., but it does not look in the subdirectories of DIR. The only thing of note is that I was using DIR relatively, pointing to a subfolder of the folder where my php file was. No change when I changed it to the literal path.
I was just curious if there was a built in, or quick, easy way to read all the files in a folder, including all the file in the subfolders.
Thanks.
Nick

You would want to read the files in the subfolders recursively...
<pseudocode>
sub dirListing (@dir)
foreach $file (@dir)
if (isdir($file))
dirListing($file)
else
if (isgoodFile($file))
printout($file)
end sub;

The code below is a stripped (of irrelevant info) version of what I am currently running on a Apache webserver under Windows 2000 Pro. (There may be excessive BR html syntax, forum code conversion difficulty.)
<?php </br>
$dir = opendir("C:\Program Files\Apache\Apache2\htdocs"); </br>
echo "Directory Listing: <br><hr>"; </br>
while($file = readdir($dir)) </br>
{ </br>
print("<A HREF=\"$file\">$file</A><BR>\n"); </br>
} </br>
?> </br>
</br>It lists every file and folder and allows direct access to each of them through a hyperlink.
Here are the catches:
0) The results of this script are 100% dependent on the location of the script.
The 'htdocs' listing is the Apache equivalent to the IIS 'C:\intepub\www' - the root of the public html files. It serves as only the initial face of the directory listing, then its the HTTP standard once you leave the script.1) The files are hyperlinked and can be viewed with the application associated with the extension.
2) When the hyperlinked folders are clicked they are listed out through the standard HTTP directory listing, !IF! directory browsing is authorized in your configuration file. At this directory listing, PHP is out of the loop and you only can work with the standard HTTP directory listing (until you click backwards to the PHP script).
Maybe one of the above addresses your problem more clearly. If you want a PHP "Face" to each of your directory listings, here is some ways to do it...
Store the PHP script for directory browsing in a universal location and using the include() of PHP as
<?php
include("dir.php")
?>In each directory where you want the directory to be shown through PHP.
Or you can simply (if it is feasible to do so) put a standard dir.php in each of your directories and subdirectories... that references the .. (current directory) and have the main script append the "dir.php" to your hyperlink for each subdirectory if the return value of is_dir() denotes a existing directory or subdirectory. May be overkill but it will work.
It really depends, try and access these directories directly through your IIS server to ensure you can get a listing of them. If you want a PHP "face" to your directory listing and not the standard HTTP listing, then you can try one of the solutions above based on the amount of directories you are working with you can determine which is more feasible and perhaps be able to optimize it a bit for speed / resource efficiency.
To answer your other question, the only PHP function, to my knowledge, that reads all files in a directory / folder is readdir($dir), where $dir is the resulting value from the command
$dir = opendir($current_dir) and $current_dir is a string that represents the path of the directory that you want to access.Let me know if you are still having problems.
IR

I think that I've got it. I'll have to tweak it for what I'm doing, but it should work. Thanks for the help.
Nick

![]() |
![]() |
![]() |

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