Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Need Some Help In File Browser, listing all sub folders and files in them.
apple
post Mar 26 2008, 05:35 PM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 9-August 06
Member No.: 28,049



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.


CODE
<?
$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);
?>
Go to the top of the page
 
+Quote Post
galexcd
post Mar 26 2008, 05:47 PM
Post #2


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



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:

CODE
<?
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.

This post has been edited by alex7h3pr0gr4m3r: Mar 26 2008, 05:55 PM
Go to the top of the page
 
+Quote Post
apple
post Mar 26 2008, 09:16 PM
Post #3


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 9-August 06
Member No.: 28,049



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.
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 26 2008, 09:46 PM
Post #4


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
galexcd
post Mar 27 2008, 12:09 AM
Post #5


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



QUOTE(apple @ Mar 26 2008, 02:16 PM) *
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.

QUOTE
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 www.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 www.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 www.example.com/?dir=images rather than www.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:

CODE
<?
$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:

CODE
<?
$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);
?>
Go to the top of the page
 
+Quote Post
apple
post Mar 27 2008, 09:37 AM
Post #6


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 9-August 06
Member No.: 28,049



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
Go to the top of the page
 
+Quote Post
galexcd
post Mar 27 2008, 04:37 PM
Post #7


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



QUOTE(apple @ Mar 27 2008, 02:37 AM) *
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.
Go to the top of the page
 
+Quote Post
apple
post Mar 27 2008, 05:22 PM
Post #8


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 9-August 06
Member No.: 28,049



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

http://localhost/test/fb/fb4.php?dir=images/

This page prints only this "Unable to open"...
it can not open any subdirectory.
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 27 2008, 05:26 PM
Post #9


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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