PHPBuilder - File Management - Tree Hierarchy Looks



RSS Twitter
Tips Application Architecture

File Management - Tree Hierarchy Looks

by: Alexander Yanuar Koentjara
|
December 14, 2000

If you want to control your site from afar, and you don't have any FTP account on that site ... Use my code to :

1. Browse remote file system in Tree Hierarchy Looks
2. Create/Change directory
3. Read, Upload, Download, Delete File

But make sure that you have to make this page secure, so noone can erase or browse your files like child ... Using WWW-Authenticate header is a good start :)

Code : adm_filem.php (stands from Admin File Management)
<?
/*
Title : 
   Admin File Management v1.0

Original code written by : 
   Alexander Yanuar Koentjara
   lexzeus@hotmail.com
   www.geocities.com/lexzeus/
*/
set_time_limit(0);
print "<HTML>\n<BODY>\n";

$my_data = "";
$f_dirs = stripslashes($f_dirs);
$f_files = stripslashes($f_files);

if ($f_action && $f_action!="DOWNLOAD")
   {
   if ($f_action == "UPLOAD")
      {
      if (substr( $f_dirs, strlen($f_dirs)-1, 1)!="/" && substr( $f_dirs, strlen($f_dirs)-1, 1)!="\\")
         $my_data = $f_dirs."/".basename($f_upload_name);
      else
         $my_data = $f_dirs.basename($f_upload_name);
      if (copy($f_upload, $my_data))
         {
         $my_data = "File is uploaded into $my_data";
         unlink($f_upload);
         }
      else
         $my_data = "<Font color=#DD0000>Error while uploading the file, the possibility reason :
         
</Font> <LI> The file is too big <LI> You don't have permission to create a file in directory $f_dirs"; } if ($f_action == " VIEW ") { $fp = fopen($f_files,"r"); while (!feof($fp)) $my_data .= fread($fp,10240); fclose($fp); $my_data = "<u>Source code of $f_files</U>
". htmlspecialchars($my_data) ."
"; } if ($f_action == "DELETE") { if (unlink($f_files)) $my_data = "$f_files is deleted"; else $my_data = "$f_files cannot be deleted, maybe the file is not belong to LaSH"; } if ($f_action == "CHANGE") { $my_data = "Change directory to $f_chdir"; $directory = $f_chdir; } if ($f_action == "CREATE") { if (substr( $f_dirs, strlen($f_dirs)-1, 1)!="/" && substr( $f_dirs, strlen($f_dirs)-1, 1)!="\\") $my_data = $f_dirs."/".$f_mkdir; else $my_data = $f_dirs.$f_mkdir; if (@mkdir($my_data,0770)) $my_data = "Directory $my_data is created"; else $my_data = "<Font color=#DD0000>Error occured while trying to create directory $my_data, the possibility reason :
</Font> <LI> The directory $f_dirs is exist already <LI> You don't have permission to create on $f_dirs <LI> $f_dirs is not a valid directory name"; } if ($f_action == "REMOVE") { if (@rmdir($f_dirs)) $my_data = "Directory $f_dirs is removed"; else $my_data = "<Font color=#DD0000>Directory $f_dirs cannot be removed, the possibility reason :
</Font> <LI> The directory $f_dirs is not exist <LI> The directory $f_dirs is not empty <LI> Maybe you do not have permission to remove $f_dirs"; } } ?> <CENTER> <? if ($my_data."x"!="x") print " <TABLE Border=1 cellpadding = 20 BGCOLOR=#ffffff width=95%> <TR> <TD> $my_data </td> </tr> </table>

"; ?> <TABLE Border=1 cellpadding = 20 BGCOLOR=#DDDDFF> <TR> <TD> <CENTER>

<FONT COLOR=#60000>File Management</FONT>

</CENTER>
<? $all_files = array(); $all_dirs = array(); function x_dir($param) { GLOBAL $all_files; GLOBAL $all_dirs; $all_dirs[count($all_dirs)] = $param; if (substr($param,strlen($param)-1,1)!='/' && substr($param,strlen($param)-1,1)!='\\') $param .= "/"; $dir = opendir($param); $files = array(); while ($ff=readdir($dir)) { if ($ff!=".." && $ff!=".") { $ff = $param . $ff; if (is_dir($ff)) $files[count($files)]="0".$ff; // directory is marked with "0" else $files[count($files)]="1".$ff; // file is marked with "1" } } closedir($dir); if (count($files)==0) return; sort($files); print "<table cellspacing=0 cellpadding=0 border=0>"; $pic="cross.jpg"; for ($i=0;$i<count($files);$i++) { if ($i+1==count($files)) $pic="cross2.jpg"; if (substr($files[$i],0,1)=="0") $attr="face=verdana color=#800000 size=1>"; else { $attr="face=arial color=black size=1>"; $all_files[count($all_files)] = substr($files[$i],1,1000); } //<img src='$pic'> $txt = split('/',$files[$i]); $txt = $txt[count($txt)-1]; print "<TR><td valign=top background='cross3.jpg'><img src='$pic'></td>". "<td valign=top rowspan=2><font $attr $txt </font>"; flush(); if (substr($files[$i],0,1)=="0") x_dir(substr($files[$i],1,1000),$margin); // recursive call if ($i+1==count($files)) print "</td></tr> <tr><td valign=top height=1></td></tr>\n"; else print "</td></tr> <tr><td valign=top background='cross3.jpg'><img src='blank.jpg' height=1></td></tr>\n"; } print "</table>"; } function list_dir($dir) { print "<Font Color=#80000 Face='Verdana'><U>$dir</U></Font>
"; print "<table cellpadding=0 cellspacing=0><tr><td><img src='cross3.jpg'></td></tr>"; x_dir($dir); } if (!$directory) $directory="."; list_dir($directory); print "<font face=\"courier\" size=1>"; for ($i=0;$i<50;$i++) print "&nbsp;"; print "</font>\n"; ?> <Font face="Verdana" Size=2> <FORM Action="adm_filem.php" method="POST" enctype="multipart/form-data"> <HR> <Font color=#800000 face="Verdana" Size=4>Action :</font>

Directory :
<SELECT NAME="f_dirs"> <? for ($i=0;$i<count($all_dirs);$i++) print " <OPTION value='$all_dirs[$i]'> $all_dirs[$i] \n"; ?> </SELECT> <input type=submit name="f_action" value="REMOVE">
<TABLE cellpadding=0 cellspacing=0 width=100%> <TR> <TD> Create directory </TD> <TD> &nbsp; </TD> </TR> <TR> <TD> <input type=text name="f_mkdir" value="new_folder" size=40> </TD> <TD align=right> <input type=submit name="f_action" value="CREATE"> </TD> </TR> <TR> <TD> Change directory to </TD> <TD> &nbsp; </TD> </TR> <TR> <TD> <input type=text name="f_chdir" value="<? print $DOCUMENT_ROOT; ?>" size=40></TD> <TD align=right> <input type=submit name="f_action" value="CHANGE"></TD> </TR> <TR> <TD> Upload file into directory </TD> <TD> &nbsp; </TD> </TR> <TR> <TD> <input type=file name="f_upload" size=28> </TD> <TD align=right> <input type=submit name="f_action" value="UPLOAD"></TD> </TR> </TABLE>

File :
<SELECT NAME="f_files"> <? for ($i=0;$i<count($all_files);$i++) print " <OPTION value='$all_files[$i]'> $all_files[$i] \n"; ?> </SELECT><font size=5>&nbsp;
<TABLE width=100%> <TR> <TD align=left> <Input type=submit name="f_action" Value=" VIEW "> &nbsp; </TD> <TD align=center><Input type=submit name="f_action" Value="DOWNLOAD"> &nbsp; </TD> <TD align=right> <Input type=submit name="f_action" Value="DELETE"></font> </TD> </TR> </TABLE> <input type="hidden" name="directory" value="<? print $directory; ?>"> <HR> </FORM> </Font> </TD> </TR> </TABLE> </CENTER> </BODY> </HTML> <Script language='javascript'> location.href="#top"; <? if ($f_action=="DOWNLOAD") print " setTimeout('location.href=\"adm_filem_download.php?f_files=". urlencode($f_files) ."\";',1000);"; ?> </script> Code : adm_filem_download.php <? $f = @fopen($f_files,"r"); if (!$f) print "<HTML><BODY>

Failed to download $f_files !!!

</BODY></HTML>"; else { header("Content-Type: application/octet-stream"); header("Content-Length: ".filesize($f_files)); header("Content-Disposition: attachment; filename=" . basename($f_files)); $data=""; while (!feof($f)) $data.=fread($f,64000); fclose($f); print $data; } ?> The last thing you have to do is to define 1. Draw cross.jpg, is a |- picture in 29x15 pixels 2. Draw cross2.jpg, is a |_ picture in 29x15 pixels 3. Draw cross3.jpg, is a | picture in 29x15 pixels Those picture is needed so you can see nice tree hierarchy ... Sorry, my english is so bad ... Alexander Yanuar K. System Developer of www.globalsources.com

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Alexander Yanuar Koentjara

Comment:



Comment:

(Maximum characters: 1200). You have characters left.