Location: PHPKode > scripts > Simple Server File Browser > simple-server-file-browser/file_browse.php
<?php

/*
 * Orginal file based on:
 * File Browser Class by Andy Gajetzki hide@address.com
 *
 * Author Wojciech Bajon    mail:  bato3 server: wp.pl
 *
 */
 
 //Descriptopn:
 
 //Appearance:
 
 /*
    hash(string path) - function to acces protect to dir;
    __image(string filename) - return file icon
    head() -- includes DOM function to presentatnion file
    foot()
    dir() - print dir line
    __img() - print file line - image
    __a() - print file line - other

  */
  
  //Usage:
  /*
    $aa=new filebrowser();

    if($aa->hash($_GET['dir'])==$_GET['hash'])
      $aa->list_files($_GET['dir']);
     else
      $aa->list_files('.');

*/

class filebrowser{ 
  //config vars
  var $imagepath = "/icons";  //##Relative to http://localhost/
  var $dateformat='Y-m-d H:m';//'F j, Y, h:i a'; //to date()
  var $hidden='Ukryj'; //to JS/DOM
  var $openInNewWindow='Otwórz w nowym oknie'; //to JS/DOM
  var $serwname='http://localhost/starostwo/'; // to JS - server name witch path
  //
  //==================================================================================
  function hash($string){//REMEMBER, this crypt based on secret password and secret alghoritm
    return substr(base_convert(md5($string.' ::random sting::
      ->  REMEMBER, this crypt based on secret password and secret alghoritm <-
      '.$string),16,32),0,8);
  }
  //==================================================================================
  //config function ;)
  function __image($filename){
    $fileext=explode(".", strtolower($filename));
    //icons form Apache
    if (($fileext[(sizeof($fileext)) - 1]) !== "DONTMATCHTHISPLEASEORELSE") {$image = "blank.gif";}
    if (($fileext[(sizeof($fileext)) - 1]) == "txt") {$image = "text.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "zip") {$image = "zip.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "html") {$image = "text.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "htm") {$image = "text.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "pdf") {$image = "pdf.gif";  }
     else if (($fileext[(sizeof($fileext)) - 1]) == "ps") {$image = "ps.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "php") {$image = "php.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "gif") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "jpg") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "jpeg") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "png") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "bmp") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "tiff") {$image = "image2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "mp3") {$image = "sound2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "vav") {$image = "sound2.gif";}
     else if (($fileext[(sizeof($fileext)) - 1]) == "mid") {$image = "sound2.gif";}
   return $image;
  }
  //==================================================================================
  function head(){
    ?><html>
<head>
<style>
a{	cursor : pointer; color:blue;}
div.floater {
	//width: 40%;
	margin: 10px;
	margin-left: 10px;
	padding: 10px;
	border: 1px solid #B0BDEC;
	background-color: #D5DFF5;
	font-size: 90%;
	clear: right;
}
.toc{text-align:right;}
#toc {
	position: fixed;
	//position: static;
	top: 0px;
	right: 100px;
	background: #DEE7F7;
	width: 400px;
	padding: 0;
	max-height: 100%;
	//overflow: auto;
}
</style>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-2">
<script language="javascript">
<!--
<?php
  print "var close='".$this->hidden."';\nvar open='".$this->openInNewWindow;
  print "';\nvar servname='".$this->serwname."';\n";
?>
  function zwin(){
    tr = document.getElementById("tr1");
    td = document.getElementById("td_obr");
    tr.removeChild(td);
   return true;
  }
  //============================================
  function pocz(){
    if(document.getElementById("td_obr")!=null)
      zwin();
      
    td = document.createElement("td");
    td.id="td_obr";
    td.setAttribute("valign", "top");
    td.setAttribute("width", "1");
    
    a1 = document.createElement("a");
    a1.id = 'hidden';
    a1.setAttribute("onclick", "return zwin();");
    a1.innerHTML=close;
    
    div = document.createElement("div");
    div.id='toc';
    div.appendChild(a1);
    //div.firstChild.nodeValue='ala';
    
    td.appendChild(div);
    
    tr=document.getElementById("tr1");
    tr.appendChild(td);
    
   // br = document.createElement("br");
   // div.appendChild(br);
  
    return div;
  }
  //============================================
  function img(id,size){
   
    div = pocz();
    
    tmp=document.getElementById(id);
    
    div2 = document.createElement("div");
    div2.setAttribute("class", "floater");
    div2.innerHTML = "&lt;img src='"+tmp.href.substring(servname.length,tmp.href.length)
      + "' border='0' alt='" + tmp.id + "' title='" + size + "'/&gt;";
    div.appendChild(div2);
    
    div3 = document.createElement("div");
    div3.innerHTML="<center><a href='"+ tmp.href + "' target='_blank'>"
      + open + "</a></center>";
    div.appendChild(div3);
    
    
    div2 = document.createElement("div");
    div2.setAttribute("class", "floater");
    img2 = document.createElement("img");
    img2.id = 'img1';
    img2.setAttribute("border", "0");
    //img2.setAttribute("width", "100%");
    img2.setAttribute("src", tmp.href);
    div2.appendChild(img2);
    div.appendChild(div2);
    
    
    return false;
  }
  //===========================================
  function a_href(id,size){
    
    
    div = pocz();
    
    tmp=document.getElementById(id);
    
    div2 = document.createElement("div");
    div2.setAttribute("class", "floater");
    div2.innerHTML = "&lt;a href='" + tmp.href.substring(servname.length,tmp.href.length)
      + "' title='" + tmp.id + " (" + size + ")'/&gt;";  
    div.appendChild(div2);
    
    div3 = document.createElement("div");
    div3.innerHTML="<center><a href='"+ tmp.href + "' target='_blank'>"
      + open + "</a></center>";
    div.appendChild(div3);

    return false;
  }
//-->
</script>
</head>
<body><a name="top"></a><table width="100%" border="0" id="tab1">
 <tr id="tr1">
  <td width="*" id="td_link" valign="top">
  <?php
    print("<table width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\">");
  }
  //==================================================================================
  function foot(){
    print "</table>\n";
    ?>  </td>
 </tr>
</table>
</body>
</html><?php
  }
  //==================================================================================
  function row($path, $file,$row_no=0){
    if (($file !== ".") && ($file !== "..")){
      if(is_dir("$path/$file"))
        print $this->dir($path,$file);
      if(is_file("$path/$file"))
        print $this->file($path,$file);
    }
  }
  //==================================================================================
  //==================================================================================
  //==================================================================================
  var $dirhandle; //private:
  //==================================================================================
  //==================================================================================
  function __read_dir($path){
    $this->dirhandle=Array();
    $handle=opendir($path);
    while ($file = readdir($handle))
      $this->dirhandle[] = $file;
    sort($this->dirhandle);
    closedir($handle);
    clearstatcache ();
  }
  //=================================================================================
  function is_img($filename){ //is image
    $fileext=explode(".", strtolower($filename));
    $img=Array('jpg','jpeg','bmp','tiff','tif','gif','png');
    if(in_array($fileext[(sizeof($fileext))-1],$img))
      return true;
     else
      return false;
  }
  //=================================================================================
  function __img($path,$file){
    $size =(ceil(filesize("$path/$file")/1024))." kb";
    $href=$path."/".$file.'"'."id='$file'".' onclick="return img(' . "'$file', '$size'" . ');';
    
    return "\n".'<tr><td width="1%" align="left">
      <a href="'.$href.'"><img 
      hspace="5" width="20" height="22" src="'.$this->imagepath
        .'/'. $this->__image("$path/$file") .'" border="0"></a>'."
      <td width=\"33%\" align=\"left\"><a href=\"$href\">$file</a></td></td>
      <td width=\"20%\" align=\"right\" nowrap> $size </td>
      <td width=\"5%\" align=\"right\">&nbsp;</td>
      <td width=\"20%\" align=\"right\" nowrap>"
        . (date($this->dateformat, (filectime("$path/$file")))) .  " </td>"
      //."<td width=\"29%\" align=\"right\">&nbsp;</td>"
      ."</tr>";
  }
  //=================================================================================
  function __a($path,$file){

    $size =(ceil(filesize("$path/$file")/1024))." kb";
    $href=$path."/".$file.'"'."id='$file'".' onclick="return a_href(' . "'$file', '$size'" . ');';
    
    return "\n".'<tr><td width="1%" align="left">
      <a href="'.$href.'"><img 
      hspace="5" width="20" height="22" src="'.$this->imagepath
        .'/'. $this->__image("$path/$file") .'" border="0"></a>'."
      <td width=\"33%\" align=\"left\"><a href=\"$href\">$file</a></td></td>
      <td width=\"20%\" align=\"right\" nowrap> $size </td>
      <td width=\"5%\" align=\"right\">&nbsp;</td>
      <td width=\"20%\" align=\"right\" nowrap>"
        . (date($this->dateformat, (filectime("$path/$file")))) .  " </td>"
      //."<td width=\"29%\" align=\"right\">&nbsp;</td>"
      ."</tr>";
  }
  //==================================================================================
  function dir($path, $file){ //directory show
    $href="?dir=$path/{$file}&hash=".$this->hash("$path/$file");
    return "\n<tr ><td width=\"1%\" align=\"left\">
      <a href=\"$href\"><img hspace=\"5\" width=\"20\" height=\"22\" src=\"".$this->imagepath
        ."/folder.gif\" border=\"0\"></a>
      <td width=\"33%\" align=\"left\"><a href=\"$href\">$file</a></td></td>
      <td width=\"20%\" align=\"right\" nowrap>&lt;DIR&gt;    </td>
      <td width=\"5%\" align=\"right\">&nbsp;</td>
      <td width=\"20%\" align=\"right\" nowrap>"
        .(date($this->dateformat, (fileatime("$path/$file")))) . "</td>"
      ."</tr>";
  }
  //==================================================================================
  function file($path,$file){   //file show
    return ($this->is_img("$path/$file")?$this->__img($path,$file):$this->__a($path,$file));
  }
  //==================================================================================
  function list_files($path){
    if(is_dir($path)){
      $this->__read_dir($path);
      $this->head();
      for($i=0;$i < count($this->dirhandle); $i++)
        $this->row($path,$this->dirhandle[$i],$i);
      $this->foot();
    }
  }
  //==================================================================================
  //==================================================================================
}; //end class filebrowser

/*
    $aa=new filebrowser();
    
    if($aa->hash($_GET['dir'])==$_GET['hash'])
      $aa->list_files($_GET['dir']);
     else
      $aa->list_files('../..');
      
*/
?>
Return current item: Simple Server File Browser