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

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP 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.


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

Reply

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:

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.

 

 

 


Reply

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.

Reply

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.

Reply

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

Reply

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

Reply

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

Reply

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

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

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

Reply

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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : file browser listing folders files

  1. Linux/ Apache /mod_rewrite Issue - Error when accessing a file (4)
  2. Php Configuration File - "config.php" (16)
    I did create this topic mainly because I want to know everything about that configuration file. I
    will post other replies if I want to know more depending on your experience. Is this code correct
    for that file: CODE <? $host="localhost"; $dbname="XXX";
    $dbuser="XXX"; $dbpass="XXX";
    $connection=mysql_connect($host, $dbuser, $dbpass) or
    die(mysql_error()); mysql_select_db($dbname) or
    die(mysql_error()); ?> Add your suggestions or i...
  3. Php Source Code Unveiled In Browser? - is that possible? (7)
    I am quite new to PHP and this concern came to my mind after playing around a bit with it... When
    PHP is not correctly configured on the web server the source code of a php file we try to access
    through a browser will be shown instead of the result of the code itself. This will normally not
    happen when PHP is working properly, but I was just wondering if it could still be possible to see
    that code if a user wanted to or if something on the server failed. This would for example expose
    sensitive information like mysql passwords and so on... Is anything like that possib...
  4. Getting List Of Directories And Files Using Php - PHP Function for Directory and File List (6)
    is there a php function that lists the content of some folder.... example: /New folder new.txt
    left.gif download.zip dc.exe ....so is there..? /rolleyes.gif' border='0'
    style='vertical-align:middle' alt='rolleyes.gif' /> ...
  5. Include File.php?id=something - using the include() function (13)
    Well, I am making a full CMS system for my site, and want to make the index.php file to include the
    view.php?id=1 file. I tried with this code, but it didn't work: CODE <?php include
    'view.php?id=1' ?> This is the error I get: CODE Warning:
    main(view.php?id=1) [function.main]: failed to open stream: Invalid argument
    in C:\server\xampp\htdocs\test\index.php on line 1 Warning:
    main() [function.include]: Failed opening 'view.php?id=1' for inclusion
    (i...
  6. Script: Php Jukebox - A one file script! (4)
    This scripts is so simple, you dont need to edit ANY of it! All you have to do is make a folder
    called 'songs' and put some audio files in it. Here is the whole page, I named it index.php
    and put it in a folder called 'music': CODE <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
    <title>PHP jukebox</title> </head> <body> <!-- ©2005 Craig
    lloyd. All rights reserved. Visit cragllo.com for more sc...
  7. Updating Php File Through A Web Form - (5)
    Hello, i'm not sure if this can be done with php or not but what i need is a way to make an php
    file that have an html form on it and it will take the info you put in to that form and write it to
    an existing php file, for example: if i have the file news.php and the file news_update.php. if you
    went to news_update.php you would get an form with a text area for you to write a new addition for
    the news.php file and when you hit submit it will add what you typed in the form to the file
    news.php. If this is going to be a big code or a hard one to make but some one think...
  8. Download Script For Mp3 Files - (0)
    Hello, I'm looking for a download script for sound files (e.g. mp3, avi, wma, and other ones).
    i have found a few download scripts but they would not work for sound files for some reason. also
    this will not be used for allowing downloading of illegal or riped music, what i will be using this
    script for is i'm making a site for my church and the pastor wants to be able to recored the
    services and then have me upload them to the site so that the church members can download them for
    what ever reason. If some one could tell me how to make one or could show me a plac...
  9. Change Permission With Php Code - code to change files' and folders' permissions? (3)
    As everyone know, there two ways (that I can think of) to change files' and directories'
    permissions. One is to change it in your cPanel's Disk Manager and the other is with an FTP
    client that supports chmod. Well, I'm doing something for my site that requires files to have
    full permissions (Execute, Write, and Read on all three groups). At first, I thought that if I made
    the directory 777, then every file created in that directory will be 777 as well. I'm wrong. An
    alternative to doing this is to change each file permission myself, but that would be...
  10. File Checker-how To Check File Whith Html Through Html? - (2)
    edit:sorry for the mistake it is php not html and its with not whith my code checking script= CODE
    <?php $file = '$CHECK'; if (file_exists($file)) {
        echo "The file $filename exists"; } else {     echo "The file $filename
    does not exist"; } ?> my question is how to check the file whith html example:on a page
    a text box is provided and a button the user writes a file name (or website) the user clicks on the
    submit button then it checks and show it (with the code above) i got the code C...
  11. Php And Flash Image Gallery - Need some help in creating or editing an xml file while viewing some o (5)
    Hello there and thanks for the helping hand you are offering. PHP newbie here! /ph34r.gif"
    style="vertical-align:middle" emoid=":ph34r:" border="0" alt="ph34r.gif" /> So here is my problem:
    On my website I have a flash image gallery.The way the gallery works is by uploading pictures in a
    folder and editing? an xml file.(pics.xml) where it adds the following code when you upload a
    picture: CODE <pictures>     <image location="nameofpicture1.jpg"
    desc="" />     <image location="nameofpicture2.jpg" desc="" /&#...
  12. No File Extension - (3)
    On MediaWiki, the URL of the content is http://YOURWIKI.com/index.php/Blah Is it possible to
    create a page or two that doesn't have a file extension? If so, how is it done?...
  13. Grabt Access To My Protected Files - grabt access to my protected files (2)
    Hi all, I am sure all of you are great programers but First i am no code programmer i am just
    trying to learn to run my sites, will the problem is i got a code from the web to protect my files
    ,1st i want to know how to work it out then i will try other things but can't let it go without
    a fight thanx all php: CODE <?php if ( ! defined( 'myname' )
    ) {         print "You cannot access this file directly.";         exit(); }
    customer data here ?> now i can't access my files what is the meth...
  14. File Upload - File upload (1)
    I need to add a facility on my customer's website so his clients can send him jobs, typically
    5mb - 50mb. I've looked around the web and researched this, and tried a few tests (failed), but
    my brain's beginning to hurt. Could someone please tell me the best way to go about this,
    please. The site is done in Flash, but I'm sure a link to an html page would be ok if necessary....
  15. Security Issue Writing Files - Security issue writing files (1)
    Hi, first, sorry about my english. i am a beginner with php and i have some question about writing
    files using php in a shared hosting. is a risk?, use database to store data is a better way? i just
    want make an interface (in php) that write the data in a .html extension file to show to everybody
    the html page and just the php interface is to the content manager. thanks in advance ...
  16. <?php ?> Unique Visitors Script - Flat file unique visitors script (no sessions) (2)
    This is really simple script. Well at least this part is, but it could be extendable. Only problem
    is that it's not really for massive websites with hundread of visitors a day, but rather for
    small ones. But it is a good script to figure out how to make a visitor counter script. Anyway
    here's the snippet. CODE <?php function getVisits($variable) {
        $visits = array();     if ($handle =
    opendir('stats/')) {     while (false !== ($file =
    readdir($handle))) {  ...
  17. [php]simple Flat File Text Manipulator - Example on how to use forms to write to files in PHP (3)
    I made a simple flat file text editor, that can show you probably how simple it is to use forms with
    php and write that data to file. This example has 2 files, submit.php, and postit.html. Submit.php
    is used to write title, and some text, and add html tags, and paragraph tags where new paragraphs
    are. Here's the file with comments. I think that HTML really doesn't need some more
    explaining. CODE Title: <br /> <input type="text"
    name="title" size="53"> <br /> Text: <br />
    <textarea nam...
  18. Forms, Text Files, And Php For A Signature Generator. - Help a little. (1)
    Hello everyone! I am in need of some code a for a signature generator I am making. I am using
    BuffaloHELP's code for the php file, now I am trying to improve that code by making a form in a
    html file that will have the user say what is on the sig! But now, I need help getting the form
    data that is posted by the user to get into that sig! There is a file, sig.txt, where that tells
    the php file what text will go on the sig. But how can I make the form data in the html file go into
    the text file so it will go onto the sig? You might want to read BuffaloHELP&...
  19. Edit .txt File In Ftp Via Webpage - file on external ftp (2)
    Right Im new here and stuggling with a problem im having. I've currently got a Login Script to
    login to a external ftp and it displays the folders contents, however i need it so it echos a
    specific file (coursedata.cfg) into a formarea which you can then edit the file and when u click
    save it overwrites the file with the new one. Im really quiet getting annoyed with it xD as
    everything i do ends up trying to include the file from the webserver the script is hosted on and
    not the external ftp source. thanks Full Code Bellow Simple FTP Manager body { fon...
  20. Reading Files From Directory To Array, And Using $_get To Get Them - Simple way to manage lot's of files (2)
    Some user posted a similar problem i had when i tried to figure out how to update content on my
    website in less work as possible. This is just part of the "big" plan i have for my site but it can
    be helpfull to you guys if you like FlatFile. I hope that mod's don't mind me posting the
    same code here and on their forums, couse if they do i'll delete it from their forums
    /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> So
    here's the situation. Let's say you have a folder called 'myfilesdirectory' on your
    s...
  21. Php Help Needed Including File In A Page. - (2)
    i'm a noob in php programming, i can understand and modify php programs, but i dont know to
    write on my own. So please somebody who is well versed in php help me. My need is, I'm
    currently builiding a knowledge base website , i've my own design for the website, check here,
    http://laschatz.info/kzone/ Each page in the left hand site has a tree navigation of all the
    topics available. Since this information must be same in all the pages, presently I need to change
    all the pages after adding a new category. Could you please help me in such a way that I can ad...
  22. Help Php: How To Load String From Text File (solved) - Loading string from text file when you click on your link (9)
    I learned the way to load other files with the code posted on this forum. Now i wanted to try
    something for my side menu. I am calling this a string, --> $tekst , maybe it's
    called something else i'm not sure Now let's say i have a file called details.txt In that
    file i would like to have something like this $detailsaboutphp1 = ("details details details
    1"); $detailsaboutphp2 = ("details details details and even more details 2"); How to make a
    code that loads those $strings on click of a mouse. When we tried to load extern...
  23. I Need Help With File Edit In Php - with ftp (6)
    Currently i have CODE <?php if($_POST['user']=="" or
    $_POST['pass']=="" or $_POST['host']==""
    or $_POST['root']=="" or
    !isset($_POST['run'])){  print('<form method=post
    name=form>');  print('FTP username <input type=\'text\'
    name=user value=""><br>');  print('FTP password <input
    type=\'password\'...
  24. Writing To Files And Such - (3)
    Ok lets say i have a config.php file, i want to edit something without open it, i know every line
    number and that, now how would i do this? I will show you a example: CODE <?php
    $CONFIG['user']            =    'root';
    $CONFIG['server']        =    'localhost';
    $CONFIG['pass']            =    '******';
    $CONFIG['db']            =    '******';
    $CONFIG['install']        =    '1';
    $CONFIG['lang']       ...
  25. Help Needed With Directory/file Listing Code Infinite Loop - Made an infinite loop but why is this so? (5)
    Hi all ive got a small and simple (for the moment atleast /unsure.gif"
    style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> )file and directory
    listing script in php as follows CODE $dir = "."; $num = 0; $file =
    scandir($dir); while($file = scandir($dir)){     echo
    $file[$num];     echo "<BR>";     $num = $num + 1;     
    }; the concept is simple enough, the directory to start with is the current one, so scan this
    directory and wh...
  26. Files Required? - (5)
    I want to learn php and I am already on the first steps to doing it. I have set up my server using
    easyPHP as well as XAMPP which is a package containing FileZilla, Apache and MySQL. Other than
    easyPHP, XAMPP and MySQL, what other softwares are required to be set up for me to successfully
    learn how to program with PHP?...
  27. Display Random File In A Directory - how to display a random file from a set directory. (9)
    hi, could someone please help me with this? I have some files in a directory and i want to know how
    i can randomly display link/s to one or more of the files in my directory for download. But it must
    not at any time display index.php which is also in the directory with the downloads. Thanks in
    advance for any help given /unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0"
    alt="unsure.gif" />...
  28. How To Sort Files Of A Directory using Php - (11)
    The following code displays the files of folder...but they are displaced by the order of adding... i
    want to sord the files / folders alphabetically and sord by accending order and by decending
    order.. can some one help me. $path = ""; $dir_handle = @opendir($path) or
    die("Unable to open $path"); echo "Directory Listing of $path "; while($file =
    readdir($dir_handle)) { if(is_dir($file)) { continue; } else
    if($file != '.' && $file != '..') { echo " $fi...
  29. File Format Unknown! (wap) - someone PLEASE HELP! (6)
    hi people :-) listen please can someone help me find the error in this wapsite page, its driving me
    nuts! It works fine on some phones but not on others (giving a file format unknown error) and im
    having serious problems finding the error but i bet its something stupid/simple. CODE <?php
    include ("config.php"); include ("core.php");
    header("Content-type: text/vnd.wap.wml"); header("Cache-Control:
    nostore, nocache, mustrevalidate"); print "<?xml version=\"1.0\&#...
  30. Displaying Files Of A Directory - (2)
    I want to display the contents of a directory.. i have the following code.. It gives the output in
    one column only... like file1 file2 file3 file4 . . . . . Since there are lot of files so this
    column gets very long..i want to display the x number of files in each column.. like if there are 22
    files.. then file 1 file 11 file 21 file 2 . file 22 file 3 .. . . . file 10
    file 20 This was just an example..I know it can be done by using but i dont know how to do it
    with loop. Please help me. QUOTE $dir = './'; $handle =...



Looking for file, browser, listing, folders, files,

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for file, browser, listing, folders, files,

*MORE FROM TRAP17.COM*
Linux/
Apache
/mod_rewrite
Issue Error
when
accessing a
file
Php
Configuratio
n File
"config.
php"
Php Source
Code
Unveiled In
Browser? is
that
possible?
Getting List
Of
Directories
And Files
Using Php
PHP Function
for
Directory
and File
List
Include
File.php?id=
something
using the
include()
function
Script: Php
Jukebox A
one file
script!
Updating Php
File Through
A Web Form
Download
Script For
Mp3 Files
Change
Permission
With Php
Code code to
change
files'
and
folders'
permissions?
File
Checker-how
To Check
File Whith
Html Through
Html?
Php And
Flash Image
Gallery Need
some help in
creating or
editing an
xml file
while
viewing some
o
No File
Extension
Grabt Access
To My
Protected
Files grabt
access to my
protected
files
File Upload
File upload
Security
Issue
Writing
Files
Security
issue
writing
files
<?php
?> Unique
Visitors
Script Flat
file unique
visitors
script (no
sessions)
[php]simple
Flat File
Text
Manipulator
Example on
how to use
forms to
write to
files in PHP
Forms, Text
Files, And
Php For A
Signature
Generator.
Help a
little.
Edit .txt
File In Ftp
Via Webpage
file on
external ftp
Reading
Files From
Directory To
Array, And
Using
$_get
To Get Them
Simple way
to manage
lot's of
files
Php Help
Needed
Including
File In A
Page.
Help Php:
How To Load
String From
Text File
(solved)
Loading
string from
text file
when you
click on
your link
I Need Help
With File
Edit In Php
with ftp
Writing To
Files And
Such
Help Needed
With
Directory/fi
le Listing
Code
Infinite
Loop Made an
infinite
loop but why
is this so?
Files
Required?
Display
Random File
In A
Directory
how to
display a
random file
from a set
directory.
How To Sort
Files Of A
Directory
using Php
File Format
Unknown!
(wap)
someone
PLEASE
HELP!
Displaying
Files Of A
Directory
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE