Computing.Net > Forums > Programming > PHP 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 Directory Listing

Reply to Message Icon

Name: Dr. Nick
Date: October 26, 2003 at 15:48:31 Pacific
OS: WinXP Pro SP1a
CPU/Ram: P4 2.0Ghz / 1024MB pc133
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: October 26, 2003 at 17:46:51 Pacific
Reply:

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


0

Response Number 2
Name: Infinite Recursion
Date: October 26, 2003 at 17:56:02 Pacific
Reply:

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


0

Response Number 3
Name: Dr. Nick
Date: October 26, 2003 at 22:54:57 Pacific
Reply:

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


0

Response Number 4
Name: SN
Date: October 27, 2003 at 07:08:31 Pacific
Reply:

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;


0

Response Number 5
Name: Infinite Recursion
Date: October 27, 2003 at 08:57:32 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Dr. Nick
Date: October 27, 2003 at 11:31:49 Pacific
Reply:

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


0

Response Number 7
Name: Infinite Recursion
Date: October 27, 2003 at 16:57:31 Pacific
Reply:

Good deal, hope it works out for you.

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 Directory Listing

PHP Customising Directory listing www.computing.net/answers/programming/php-customising-directory-listing/8290.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