HOME       >>       Programming

Need Some Help In File Browser listing all sub folders and files in them.


apple

Hey
I want to create a very simple file browser, so that, it reads all the sub-folders which are places in a directory, and the files inside the sub-folders (It reads only files inside sub-folders and list them in simply. ) Also, it creates a directory (any name) inside each sub folder.
My Following code reads on the files inside the main directory, it does not read the files inside the sub-folders.. I appreciate any help.


<?$path = "./";$dir_handle = @opendir($path) or die("Unable to open $path");while ($file = readdir($dir_handle)) {if($file == "." || $file == ".." || $file == "index.php" )continue;echo "<a href=\"$file\">$file</a><br />";}closedir($dir_handle);?>


galexcd

If I am understanding you correctly, you want it so when you click on a folder, it shows you the files in that directory? If so, are you changing the path when you travel inside the subfolders? If not, then that would be your problem, but if you want to show files under that dir in the same page I would recommend recursion. Something like this:

<?function display($path){$dir_handle = @opendir($path) or die("Unable to open $path");while ($file = readdir($dir_handle)) {if($file == "." || $file == ".." || $file == "index.php" )continue;echo "<a href=\"$path$file\">$file</a><br />";if(is_dir($path.$file)){echo"<table><tr><td width='50'></td><td>";display($path.$file."/");echo"</td></tr></table>";}}closedir($dir_handle);}display("./");?>

This code was tested and works like a charm.

apple

Thanks for your help.. but this code displays all the files of sub-folders on the index page.. i want to display the sub-folder files when clicked on the sub-folder.thanks.


jlhaslip

Then you will need to modify the output by building a menu from the results. Find a menu which 'opens' the sub-folder list when it is clicked. Dynamicdrive.com likely has one for that.


galexcd


Thanks for your help.. but this code displays all the files of sub-folders on the index page.. i want to display the sub-folder files when clicked on the sub-folder. thanks.


I know, you weren't very clear in your initial post so I provide two solutions.


If I am understanding you correctly, you want it so when you click on a folder, it shows you the files in that directory? If so, are you changing the path when you travel inside the subfolders? If not, then that would be your problem, but if you want to show files under that dir in the same page I would recommend recursion.


There would be two different ways of doing this. One would be just to send the path over a get modifier, but you may not want that becasue you want to be in the directory that it shows you? That may not have been too clear. Here is a basic explanation of the two solutions:

 

SOLUTION 1: you go to your website, let's say its at example.com/ and you see your file browser Lets say you have 3 directories under your root directory called "images", "stuff", and "more stuff". So when you view your site at  example.com/ you see 3 links titled "images", "stuff", and "more stuff". And if you were to click on one of the links it actually doesn't go to that directory, but only sends the data of which one you clicked on via the get variable to the same page, so clicking on "images" would bring you to

example.com/?dir=images rather than example.com/images, but you would be able to view the files in that directory. If this solution works for you here would be the code to do it:


<?$path=isset($_GET['dir'])?$dir:"./";$dir_handle = @opendir($path) or die("Unable to open $path");while ($file = readdir($dir_handle)) {if($file == "." || $file == ".." || $file == "index.php" )continue;echo "<a href=\"?dir=".$_GET['dir'].$file."/\">$file</a><br />";}closedir($dir_handle);?>

SOLUTION 2: If you want the file browser to show up on every page that doesn't have an index file on it, you can either go through the long process of adding a php file to every directory you want it in and change the $path variable to the current path but that is just tedious. There should be a way of setting directories that don't have index files to point to a certain php page, using htaccess?? Possibly? I'm not quite sure. I'm not too savvy with that file. If you can get it to point to a php file then you would just use your php code with one slight modification:


<?$path = ".".dirname($_SERVER['PHP_SELF']);$dir_handle = @opendir($path) or die("Unable to open $path");while ($file = readdir($dir_handle)) {if($file == "." || $file == ".." || $file == "index.php" )continue;echo "<a href=\"$file\">$file</a><br />";}closedir($dir_handle);?>



apple

Thank you once again for your detailed explanation.. i really appreciate it.. your first solution is very nice.. but when i click on any subdirectory.. i get the message "Unable to open".. why it is unable to open the subdirectory


galexcd

Thank you once again for your detailed explanation.. i really appreciate it.. your first solution is very nice.. but when i click on any subdirectory.. i get the message "Unable to open".. why it is unable to open the subdirectory


Copy and paste the address from the url bar when it says unable to open. There may have been an error in my code somewhere. I need to actually see this to help you figure out what is going on.

apple

okay..
Below, the file fb4.php contains your solution 1 code and is located in directory "fb" . there are many sub directories, the following adress appears after clicking on a subdirectory name "images".

LOCALHOST/test/fb/fb4.php?dir=images/

This page prints only this "Unable to open"...
it can not open any subdirectory.


jlhaslip

Umm... posting a test link to your localhost won't work for us.Can you upload it to your site account, please, and re-post the link, thanks.And, please confirm that there is an images directory under 'fb'.



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.