Jump to content
xisto Community
apple

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

Recommended Posts

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);?>

Share this post


Link to post
Share on other sites

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.
Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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);?>


Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.