Location: PHPKode > scripts > pDownload > pDownload.class.php
<?php
//Version: 0.1 BETA
class pDownload {
	//The Default Download Direction
	public $download_root = 'Dateien';
	//Settings
	public $show_dirs = false;				//Should subdirs be shown?
	public $show_php = false;				//Should .php files be shown?
	private $use_own_icons = true;			//Use own Icons?
	private $own_icons_root = './icons/';	//Folder for own icons ( icons have to be named filetype.png, e.g. pdf.png
	private $own_icons_name = 'icons';		//Folder for own icons
	private $use_own_css = true;			//Should a own CSS be applied?
	private $own_css = 'pDownload.css';		//Path to own CSS
	public $show_force_dl = true;
	//End settings
	public $filetbl_class = 'filetbl';		//CSS-Class for the filetable
	
	public $files = false;
	public $descs = false;
						   
	
	public function getFiles($download) {
		$files = array();
		foreach (new DirectoryIterator($download) as $this->fileInfo) {
			if (!$this->fileInfo->isDot()) {
				$fname = $this->fileInfo->getFilename();
				$ftype = $this->getType($fname);
				if($this->fileInfo->isDir() == true) {
					$fsize = $this->getSize($this->fileInfo->getPathname(),0);
				} else {
					$fsize = $this->fileInfo->getSize();
				}
				$mt = $this->getModTime($this->fileInfo);
				$fsize = $this->formatSize($fsize);
				$descr = $this->getTypeDescription[$ftype];
				$file = array('Name'=>$fname,'Type'=>$ftype,'Size'=>$fsize,'Mod'=>$mt,'Desc'=>$descr);
				if($ftype == 'php' and !$this->show_php) {
				} else if($this->fileInfo->isDir() and !$this->show_dirs){
				} else {
					array_push($files,$file);
				}
			}
		}	
		$this->files = $files;
		return $files;
	}
	private function getType($fname) {
		if($fname and preg_match("/\./",$fname)) {
			$array = explode(".",$fname);
			return $array[count($array)-1];
		} else {
			return 'folder';
		}
	}
	private function getIcon($ftype) {
		if($this->checkIconAv($ftype)) {
			$code = $this->own_icons_root.$ftype.'.png';
		} else if($ftype == 'folder') {
			 $code = $this->own_icons_root.'folder.png';
		} else {
			$code = $this->own_icons_root.'unknown.png';
		}
		return '<img width="64px" height="64px" src="'.$code.'" alt="'.$ftype.'" />';
	}
	public function displayFiles() {
		$files = $this->files;
		$txt = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 			"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<script type="text/javascript" src="pDownload.js"></script>
		<title>pDownload V0.1 BETA</title>';
		$txt.= (($this->use_own_css == true) ? ('<link rel="stylesheet" href="'.$this->own_css.'" />') : '');
		$txt .='
		</head>
		<body>';
		$txt .= '<table class="'.$this->filetbl_class.'">';
		$txt .= '<tr><th>Datei</th><th>Dateityp</th><th colspan="2">Gr&ouml;&szlig;e</th><th>Letzter Zugriff</th><th>Beschreibung</th></tr>';
		$cnt = 0;
		while($row = $files[$cnt]) {
			$type = $this->getType($row['Name']);
			$icon = $this->getIcon($type);
			$txt.= '<tr class="'.(($cnt%2==1) ? 'zeileA' : 'zeileB').'">';
			$txt.= '<td width="45%"><a href="forceLoad.php?filename='.$row['Name'].'">'.$row['Name'].'</a></td>';
			$txt.= '<td width="10%">'.$icon.'</td>';
			$txt.= '<td align="right" width="10%">'.$row['Size'][0].'</td><td>'.$row['Size'][1].'</td><td width="15%">'.date("H:i:s d.m.Y",$row['Mod']).'<input type="hidden" value="'.$row['Mod'].'" /></td><td width="20%">'.$row['Desc'].'</tr>';
			$cnt++;
		}
		$txt .='</table></body></html>';
		return $txt;
	} 
	private function getSize($path,$size){
      	if(is_dir($path)==0){
          	$size+=filesize($path);
      	}else{
          	$dir = opendir($path);
          	while($file = readdir($dir)){
              if(is_file($path."/".$file))
                $size+=filesize($path."/".$file);
              if(is_dir($path."/".$file) && $file!="." && $file!="..")
                $size=get_size($path."/".$file,$size);
			}
        }
      	return($size);
	}
	private function formatSize($size) {
		$measure = "Byte";
  		if ($size >= 1024)
    	{
      		$measure = "KB";
      		$size = $size / 1024;
    	}
  		if ($size >= 1024)
    	{
      		$measure = "MB";
      		$size = $size / 1024;
    	}
  		if ($size >= 1024)
    	{
      		$measure = "GB";
      		$size = $size / 1024;
    	}
  		$size = sprintf("%01.2f", $size);
  		return array($size,$measure);
	}
	private function checkIconAv($bla) {
		$arr = $this->getFiles($this->own_icons_name);
		foreach($arr as $val) {
			$fname = $val[0];
			$bname = basename($fname,'.png');
			if($bla == $bname) {
				return true;
			}
		}
		return false;
	}
	private function getModTime($fit) {
		$date = $fit->getMTime();
		return $date;
	}
	public function forceDownload($filepath) {
		if(strpos($filepath,"../")>=0 or strpos($filepath,"..%2F")>=0) {
				  return false;
		}
		header("Content-type: application/force-download"); 
		$save_as_name = basename($filepath);
		header("Content-Disposition: attachment; filename=".$save_as_name);
  		@readfile($filepath); 
		exit;
	}
	public function setDescription($filename,$descr) {
		$this->files[$filename]['Desc'] = $descr;
		return true;
	}
	public function setTypeDescription($type,$descr) {
		$this->descs[$type] = $descr;
		$files = $this->files;
		foreach($files as $key=>$val) {
			if($files[$key]['Desc'] == '' and $files[$key]['Type'] == $type) {
			$files[$key]['Desc'] = $descr;
			}
		}
		$this->files = $files;
	}
	public function getTypeDescription($type) {
		$td = $this->descs[$type];
		return isset($td) ? $td : '';
	}
}
?>
Return current item: pDownload