PHP Classes
Icontem

File: syn_class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Olafur Waage  >  synDirectory  >  syn_class.php  
File: syn_class.php
Role: Class source
Content type: text/plain
Description: synDirectory's main class
Class: synDirectory
Retrieve and sort the current directory files
 

Contents

Class file image Download
<?php
/**
 * @name SYN Directory Listing (Simple Yet Nifty)
 * @desc Directory Listing Class
 * 
 *  With this file and the original index.php that came with the package you can make
 *  a nice simple listing of what is in that directory.
 *  
 *  You can also sort by name, filesize and filetype (both ascending and descending)
 *  by clicking on the Topmost icon, "Filename" or "Size"
 *   
 * @author Olafur Waage, sinai@simnet.is 
 * @date 14.June 2007 
 * @credits, Icons made by Mark James ( http://www.famfamfam.com/lab/icons/silk/ ) 
 * 
 * Copyright © 2007 Ólafur Waage
 * Distributed under the terms of the GNU General Public License 
 */ 

/**
 * Includes the language file, since its in the same directory as this file
 */
include("lang.php");

class synDirectory
{	
	/**
	 * Outputs the main directory table, this can be customized if you know php a little.
	 * 
	 * This function needs the array that the function synDir makes if you are going to call it.
	 * Look at index.php for an example on how its done.
	 *
	 * @param array $array
	 * @return HTML table
	 */
	function mainTable($array)
	{
		global $dSize, $fCount, $dCount, $lang, $config;
		
		/**
		 * Prints out the directory name, i.e. "Index of /downloads/sources/"
		 * So this would be the header
		 */
		
		if($config["show_header"] == true)
		{
			echo "<b class='synHeader'>",$lang[$config["lang"]]["index_of"]," ",$this->getDirName(),"</b>";
			echo "<br /><br />";
		}
			
		/**
		 * This section of code is the first two lines of the table, the header and the "Parent Directory" line.
		 */
		?>
		<table class="synTable" cellspacing="5">
		  <tr class="synTopTr">
		    <td class="synTopTd"><a href="?sort=filetype&in=<?php if($_GET["sort"] == "filetype" && $_GET["in"] == "asc" || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b>T</b></a></td>
		    <td class="synTopTd"><a href="?sort=filename&in=<?php if($_GET["sort"] == "filename" && $_GET["in"] == "asc" || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?php echo $lang[$config["lang"]]["filename"]; ?></b></a></td>
		    <td class="synTopTd" align="right"><a href="?sort=modified&in=<?php if($_GET["sort"] == "modified" && $_GET["in"] == "asc" || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?=$lang[$config["lang"]]["last_modified"]?></b></a></td>
		    <td class="synTopTd" width="10"></td>
		    <td class="synTopTd" align="right"><a href="?sort=size&in=<?php if($_GET["sort"] == "size" && $_GET["in"] == "asc" || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?=$lang[$config["lang"]]["size"]?></b></a></td>
		  </tr>
		  <?php
		  	if($config["show_parent_dir"] == true)
		  	{
		  ?>
		  <tr class="synParentTr">
		      <td class="synParentTd">
		      <?php
						if($config["show_icons"] == true)
						{
					?>
		        <img src="<?=$config["class_root"]?>folder_go.png" />
		      <?php
						}
					?>
		      </td>
		      <td class="synParentTd">
		        <a href=".."><?=$lang[$config["lang"]]["parent_dir"]?></a>
		      </td>
		      <td class="synParentTd" align="right">
		        -
		      </td>
		      <td class="synParentTd" width="10"></td>
		      <td class="synParentTd" align="right">
		        -
		      </td>
		    </tr>
		<?php
		  	}
		
		/**
		 * This section of code loops through all the directories that are found within this one.
		 */
		if( is_array($array[0]) && $config["show_dir"] == true )
		{
			  foreach($array[0] as $dirData)
			  {
			    ?>
			    <tr class="synFolderTr">
			      <td class="synFolderTd">
				      <?php
								if($config["show_icons"] == true)
								{
							?>			      
			        <img src="<?=$config["class_root"]."folder.png"?>" />
			        <?php
								}
							?>
			      </td>
			      <td class="synFolderTd">
			        <a href="<?php echo $dirData; ?>"><?php echo $dirData; ?></a>
			      </td>
			      <td class="synFolderTd" align="right">
			        <?php echo date("d-M-Y H:i.s", filemtime($dirData)); ?>
			      </td>
			      <td class="synFolderTd" width="10"></td>
			      <td class="synFolderTd" align="right">
			        -
			      </td>
			    </tr>
			    <?php
			    
			    $dCount = $dCount + 1;
			  }		  	
		}
		
		/**
		 * This section of code loops through all the files that are found within this directory.
		 */
		if( is_array($array[1]) && $config["show_files"] == true )
		{  
		    foreach($array[1] as $fileKey => $fileData)
		    {
		        ?>
		        <tr class="synFileTr">
		          <td class="synFileTd">
					      <?php
									if($config["show_icons"] == true)
									{
								?>		          
		            <img src="<?php echo $this->iconImage($fileData); ?>" />
		            <?php
									}
								?>
		          </td>
		         <td class="synFileTd">
		            <a href="<?php echo $fileData; ?>"><?php echo $this->showFileExtension($fileData); ?></a>
		          </td>
		          <td class="synFileTd" align="right">
		            <?php echo date("d-M-Y H:i.s", filemtime($fileData)); ?>
		          </td>
		          <td class="synFileTd" width="10"></td>
		          <td class="synFileTd" align="right">
		            <?php echo $this->fileSizeCalc($fileData); ?>
		          </td>
		        </tr>
		        <?php
		        
		        $dSize += filesize($fileData);
		        $fCount = $fCount + 1;
		    }  
		}
		
		/**
		 * Misc statistics about the directory
		 */
		?>
		<tr class="synFooterTr">
			<td class="synFooterTd" colspan="2"><?=($dCount + 0)." ".$lang[$config["lang"]]["directories"].", ".($fCount + 0)." ".$lang[$config["lang"]]["files"]?></td>
			<td class="synFooterTd" colspan="3" align="right"><?=($fCount > 0 ? $this->byteConvert($dSize) : "")?></td>
		</tr>
		</table>	
		<?php
		
		/**
		 * Prints out misc server info, i.e. "Apache/1.3.33 Server at server.com Port 80"
		 */
		if($config["show_server_info"])
		{
			echo '<br /><small>'.$_SERVER['SERVER_SIGNATURE'].'</small>';
		}
	}
	

/* ################################################# */
/**
 * Edit only the functions below if you are knowledgeable in php
 */
/* ################################################# */

/**
 * Converts bytes into human readable form
 *
 * @param int $bytes
 * @return parsed string
 */
  function byteConvert($bytes)
  {
    $s = array('B', 'Kb', 'MB', 'GB', 'TB', 'PB');
    $e = floor(log($bytes)/log(1024));
	
    if($bytes == 0)
    {
    	return "0 B";
    } else {
    	return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));  	
    }
  }

/**
 * @desc File size calculator and parser
 * 
 * @return string, Returns the filesize in a human readable form
 */ 
  function fileSizeCalc($filename)
  {
  	$filesize = filesize($filename);

    return $this->byteConvert($filesize);
  }

  function getDirName()
  {
    $self = substr(strrchr($_SERVER['SCRIPT_NAME'],'/'), 1);
    $strpos = strrpos($_SERVER['SCRIPT_NAME'], $self);
    
    $cuttofmark = strlen($_SERVER['SCRIPT_NAME']) - $strpos;
    $cuttofminus = '-'.$cuttofmark;
    
    return substr($_SERVER['SCRIPT_NAME'], 0, intval($cuttofminus));  
  }
  
/**
 * If the config file is set for this, it will
 * display the filename without the extension. Else it will
 * display the full name
 */	 	 	   
  function showFileExtension($filename)
  {
  	global $config;
  	
		/* modifies the $entry so only the filename appears */
		if($config["show_file_extensions"] == true)
		{
			return $filename;
		}
		else
		{
			return substr($filename, 0, strpos($filename, "."));
		}
	}

/**
 * @desc Directory Array Generator 
 * 
 * @return array, Based on what is selected with $_GET or if $_GET is selected at all  
 * The array returned includes the files that are in the directory 
 */ 
  function synDir()
  {
  	global $config;
  	
    $dir = dir(getcwd());

    // Reads the directory and outputs into a simple array what we have in it.
    // Excluding the items in the first if sentance.
    $i = 0;
    while (false !== ($entry = $dir->read())) {
      if($entry == ".." || $entry == "." || $entry == "index.php" || $entry == "config.php" || $entry == "syn_class.php" || $entry == "lang.php"
				|| $entry == "avi.png" || $entry == "control_repeat.png" || $entry == "doc.png" || $entry == "exe.png" || $entry == "folder.png" || $entry == "folder_go.png"
				|| $entry == "gpl.txt" || $entry == "html.png" || $entry == "jpg.png" || $entry == "mp3.png" || $entry == "page_white.png" || $entry == "pdf.png" || $entry == "php.png"
				|| $entry == "ppt.png" || $entry == "txt.png" || $entry == "xls.png" || $entry == "zip.png")
      {
        echo "";
      }
      else
      {
        if( is_dir($entry) )
        {
        	if($_GET["sort"] == "modified")
        	{
        		$dirs[filemtime($entry).".0".$i] = $entry;
        	} else {
          		$dirs[] .= $entry;
        	}
        }
        else
        {	
        	/* goes through all files and only displays the ones*/
        	/* selected in the $config show only array */
        	if(is_array($config["show_only"]))
        	{
        		$check = substr(strrchr($entry, "."), 1);
				
        		if(in_array($check,$config["show_only"]))
						{
		        	if($_GET["sort"] == "size")
		        	{
		        		$files[filesize($entry).".0".$i] .= $entry;
		        	} elseif($_GET["sort"] == "filetype") {
		          		$fileExtension = substr(strrchr($entry, "."), 1);
		          		$fileExtension = strtolower($fileExtension);
		          		
		        		$files[$fileExtension.$entry] .= $entry;
		        	} elseif($_GET["sort"] == "modified") {
				    	$files[filemtime($entry).".0".$i] = $entry;
		        	} else {
		        		$files[] .= $entry;
		        	}
						}        		
        	} else {      	    	
	        	if($_GET["sort"] == "size")
	        	{
	        		$files[filesize($entry).".0".$i] .= $entry;
	        	} elseif($_GET["sort"] == "filetype") {
	          		$fileExtension = substr(strrchr($entry, "."), 1);
	          		$fileExtension = strtolower($fileExtension);
	          		
	        		$files[$fileExtension.$entry] .= $entry;
	        	} elseif($_GET["sort"] == "modified") {
			    	$files[filemtime($entry).".0".$i] = $entry;
	        	} else {
	        		$files[] .= $entry;
	        	}
        	}
        }
      }
      $i++;
    }
    $dir->close();

    if( !is_array($dirs) && !is_array($files))
      return FALSE;
    
    // Returns an array with filenames and directory names and file sizes
    // Sorted by filename
    if($_GET["sort"] == "filename" || !$_GET)
    { 
      if($_GET["in"] == "asc" || !$_GET)
      {
        if( is_array($files) )
          asort($files, SORT_STRING);
        if( is_array($dirs) )
          asort($dirs, SORT_STRING);     
      }
      
      if($_GET["in"] == "desc")
      {
        if( is_array($files) )
          arsort($files, SORT_STRING);       
        if( is_array($dirs) )
          arsort($dirs, SORT_STRING);   
      }
      
    return array($dirs, $files);
    }
    
    // Returns an array with filenames and directory names and file sizes
    // Sorted by filesize
    if($_GET["sort"] == "size")
    {
      if($_GET["sort"] == "size" && $_GET["in"] == "asc")
      {
        if( is_array($files) )  
          ksort($files, SORT_NUMERIC);
        if( is_array($dirs) )
          asort($dirs, SORT_STRING);
      }
      
      if($_GET["sort"] == "size" && $_GET["in"] == "desc")
      {
        if( is_array($files) )  
          krsort($files, SORT_NUMERIC);
        if( is_array($dirs) )
          asort($dirs, SORT_STRING);
      }   
    
    return array($dirs, $files); 
    }
    
    // Returns an array with filenames and directory names and file sizes
    // Sorted by filetype
    if($_GET["sort"] == "filetype")
    {
	    if($_GET["sort"] == "filetype" && $_GET["in"] == "asc")
	    {
	      if( is_array($files) )  
	        ksort($files, SORT_STRING);
	      if( is_array($dirs) )  
	        asort($dirs, SORT_STRING);
	    }
	    
	    if($_GET["sort"] == "filetype" && $_GET["in"] == "desc")
	    {
	      if( is_array($files) )  
	        krsort($files, SORT_STRING);
	      if( is_array($dirs) )  
	        asort($dirs, SORT_STRING);
	    }
	      
	    return array($dirs, $files);   
    }

    // Returns an array with filenames and directory names and file sizes
    // Sorted by file last modified unixtime
    if($_GET["sort"] == "modified")
    {
	    if($_GET["sort"] == "modified" && $_GET["in"] == "asc")
	    {
	      if( is_array($files) )
	        ksort($files, SORT_NUMERIC);
	      if( is_array($dirs) )  
	        ksort($dirs, SORT_NUMERIC);
	    }
	    
	    if($_GET["sort"] == "modified" && $_GET["in"] == "desc")
	    {
	      if( is_array($files) )  
	        krsort($files, SORT_NUMERIC);
	      if( is_array($dirs) )  
	        krsort($dirs, SORT_NUMERIC);
	    }
	      
	    return array($dirs, $files);   
    }
  }
  

/**
 * @desc Image File extension
 * @param $filename, string. The filename to be checked out what image to use based on its extension 
 * 
 * @return string, Returns the image filename corresponding to the filename parameter
 */ 
  function iconImage($filename)
  {
  	global $config;
  	
    $fileExtension = substr($filename, -4, 4);
    $fileExtension = strtolower($fileExtension);
    
    switch($fileExtension) {
    case ".mp3":
    case ".wav":
      $fileImage = "mp3.png";
      break;
    case ".jpg":
    case "jpeg":
    case ".gif":
    case ".png":
    case ".bmp":
      $fileImage = "jpg.png";
      break;
    case ".zip":
    case ".rar":
      $fileImage = "zip.png";
      break;
    case ".avi":
    case ".mpg":
    case "mpeg":
    case ".wmv":
    case "divx":
    case ".mov":
    case ".swf":
      $fileImage = "avi.png";
      break;
    case ".exe":
    case ".bat":
      $fileImage = "exe.png";
      break;
    case ".pdf":
      $fileImage = "pdf.png";
      break;
    case ".htm":
    case "html":
      $fileImage = "html.png";
      break;
    case ".txt":
      $fileImage = "txt.png";
      break;
    case ".php":
      $fileImage = "php.png";
      break;
    case ".doc":
      $fileImage = "doc.png";
      break;
    case ".xls":
      $fileImage = "xls.png";
      break;
    case ".ppt":
      $fileImage = "ppt.png";
      break;
    default:
      $fileImage = "page_white.png";
    }
    
    return $config["class_root"].$fileImage;
  }
}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products