Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP treeview, file folder browser selector

Status
Not open for further replies.

Kurt111780

Technical User
Nov 20, 2003
235
0
0
GB
Hello,

Hello, I'm looking for a php treeview or file browser script. I have a directory on our server with different folders and files. I need to allow users to select the files they want and then have a submit button to send the file names to a mySql database along with other info from a form.

Does anyone know of a file browser or treeview that would work for this? I'm not sure where to start.

Thanks,
Kurt

It's only easy when you know how.
 
Actually i dont believe there is such a construct available but it is easy enough to create one using the [blue] opendir()[/blue] and [blue]readdir()[/blue] functions.

Checkout the entries for them in the php online manual. PHP.net

You can then parse through the folder and show the files and folders in any way you like.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hello,

I was hoping for a nice treeview that I could customize but I havn't found one that I thought would work. I will look into opendir() and readdir().

Another idea is a browse button that browses a directory on the server rather than the local machine.

Kurt

It's only easy when you know how.
 
check out the PEAR classes (pear.php.net). i'm sure i have seen some pre-built classes that do something like this.

there are also definitely some freeware java applets that can do remote browsing.

or as vacunita intimates, it's not too difficult to code it on a bespoke basis - just beware of giving external access to the relevant web-pages!!
 
I got this script from the readdir() function documentation, it was user contributed...
Code:
<?php
// show directory content
function showDir($dir, $i, $maxDepth){
   $i++;
   if($checkDir = opendir($dir)){
       $cDir = 0;
       $cFile = 0;
       // check all files in $dir, add to array listDir or listFile
       while($file = readdir($checkDir)){
           if($file != "." && $file != ".."){
               if(is_dir($dir . "/" . $file)){
                   $listDir[$cDir] = $file;
                   $cDir++;
               }
               else{
                   $listFile[$cFile] = $file;
                   $cFile++;
               }
           }
       }
      
       // show directories
       if(count($listDir) > 0){
           sort($listDir);
           for($j = 0; $j < count($listDir); $j++){
               echo "
               <tr>";
                   $spacer = "";
                   for($l = 0; $l < $i; $l++) $spacer .= "&emsp;";
                   // create link
                   $link = "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=" . $dir . "/" . $listDir[$j] . "\">$listDir[$j]</a>";
                   echo "<td>" . $spacer . $link . "</td>
               </tr>";
               // list all subdirectories up to maxDepth
               if($i < $maxDepth) showDir($dir . "/" . $listDir[$j], $i, $maxDepth);
           }
       }
      
       // show files
       if(count($listFile) > 0){
           sort($listFile);
           for($k = 0; $k < count($listFile); $k++){
               $spacer = "";
               for($l = 0; $l < $i; $l++) $spacer .= "&emsp;";
               echo "
               <tr>
                   <td>" . $spacer . $listFile[$k] . "</td>
               </tr>";   
           }
       }       
       closedir($checkDir);
   }
}

if($_GET["dir"] == "" || !is_dir($_GET["dir"])) $dir = getcwd();
else $dir = $_GET["dir"];
// replace backslashes, not necessary, but better to look at
$dir = str_replace("\\", "/", $dir);

// show parent path
$pDir = pathinfo($dir);
$parentDir = $pDir["dirname"];

echo "<a href=\"" . $_SERVER["PHP_SELF"] . "\"><h3>Home</h3></a>";
echo "Current directory: " . $dir;
echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?dir=$parentDir\"><h4>Parent directory: $parentDir</h4></a>";

// Display directory content
echo"<table border=1 cellspacing=0 cellpadding=2>
<tr><th align=left>File / Dir</th>";

// specifies the maxDepth of included subdirectories
// set maxDepth to 0 if u want to display the current directory
$maxDepth = 0;
showDir($dir, -1, $maxDepth); 
?>
...It can be easily modified to your needs.

-> LuckySyringe
 
check out look for DirectoryLister (at the bottom of the page) version 1.0 is a filebrowser class, v0.7 is a drop in scripts and v0.7iconmod is modded by me - demo at but you won't see anything other than zip files there at the moment.

Kev



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks LuckySyringe and KarveR. Both those are nicer than what I came up with so far.

What have so far and would need to do with those scripts is add a form around them with a check box to select the file. The form submits the file names to another page, which then adds the names to a database.

Ultimately, I need to be able to expand each directory and select the check box before reloading the page. Need to be able to expand each folder with out reloading.

Thanks,
Kurt

It's only easy when you know how.
 
sounds like a good application for sajax.
 
see version 1.0 its a php class rather than a script, which gives folder and file browsing capability - this is still only server side, if you need something to upload files from a client you have a long way to go.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hello,

I do not need to upload files from the client. I am only passing file names from one page to another. Files already exist on the server.

I used version 1.0 of that script and added a check box and submit form and it works perfect. Its very nice. It will do for now. But it still doesn't let me select files in more than one folder at a time. It will need to load all the folders and then have client side code to expand them.

Kurt

It's only easy when you know how.
 
still sounds like ajax is your answer for a neat solution.
 
Hi KarveR,

I really like that DirectoryLister script. How could I have it load all folders at once and then have a + - button for expanding them?

Thanks,
Kurt

It's only easy when you know how.
 
For that you'd need to talk to Gabe, he wrote the thhing, I just did a bunch of stuff for security/icon mod and other tidyings. I think his mail addy is on the site.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top