Location: PHPKode > scripts > RapiDownload > rapidownload/rapidownload.class.php
<?php 
/*
Forma de uso:

<?
include("rapidownload.class.php");
$rapidownload = new rapidownload("archivos-para-bajar/");
// $verlo define si queremos ver el archivo o bajarlo directamente
if($verlo == 1){
		$rapidownload->ver_archivo("foto-hedge-y-micros.jpg");
}else{
	if(!$rapidownload->start_download("foto-hedge-y-micros.jpg")){
		echo $download->get_error();
	}
}
exit();
?>

* Descripción		  Class para bajar archivos
* Autor 			  Escrito por Sebastián Molina - hide@address.com
* Hecho 			  Jueves 27 de Julio de 2005
* Actualizaciones
	13:05 31/08/2005 - 
		He agregado un nuevo método para poder VER el archivo en vez de bajarlo, está mejor organizado en cuanto a la 
		distribución de las funciones.
		-----------------------------------------------------------------------
		Added a new method to see the file instead of download it. Functions distribution are better organized.
		
		
* Extra info:	  		
	El que quiera puede modificar a gusto este código, no está sujeto a ninguna licencia.
*/

class rapidownload{
	var $filename = "";
	var $realfilename = "";
	var $file_extension = "";
	var $error = "";
	var $ctype = "";
	var $filesize = "";
	var $galeria = "";
	function rapidownload($galeria = "",$filename = ""){
		if($filename != "")
			$this->filename = $filename;
		if($galeria != "")
			$this->galeria = $galeria;
	}
	function set_fileProperties($filename = ""){
		if($filename != ""){
			$this->filename = $filename;
		}
		$this->realfilename = realpath($this->galeria.$this->filename);
		$this->file_extension = strtolower(substr(strrchr($this->realfilename,"."),1));
		$this->filesize = filesize($this->realfilename);
	}
	function procesar(){
		if($this->filename != ""){
			if(file_exists($this->galeria.$this->filename)){
				$this->set_fileProperties();
				return true;
			}else{
				$this->error = 'El archivo especificado ('.$this->filename.') no existe';
				return false;
			}
		}else{
			$this->error = 'No se ha especificado nungun archivo para descargar.';
			return false;
		}
	}
	
	function set_filename($filename){
		$this->filename = $filename;
	}

	function set_galery($galeria){
		$this->galeria = $galeria;
	}
	function get_error(){
		if($this->error == "")
			return "Error no especificado";
		else
			return $this->error;
	}
	function get_ctype($filename = ""){
		if($filename != ""){
			$this->filename = $filename;
		}
		if($this->file_extension == ""){
			$this->set_fileProperties($this->filename);
		}
		switch($this->file_extension){
			case "pdf":	$this->ctype="application/pdf"; break;
			case "exe": $this->ctype="application/octet-stream"; break;
		    case "zip": $this->ctype="application/zip"; break;
		    case "doc": $this->ctype="application/msword"; break;
		    case "xls": $this->ctype="application/vnd.ms-excel"; break;
		    case "ppt": $this->ctype="application/vnd.ms-powerpoint"; break;
		    case "gif": $this->ctype="image/gif"; break;
		    case "png": $this->ctype="image/png"; break;
		    case "jpeg":
		    case "jpg": $this->ctype="image/jpg"; break;
		    case "mp3": $this->ctype="audio/mpeg"; break;
		    case "wav": $this->ctype="audio/x-wav"; break;
		    case "mpeg":
		    case "mpg":
		    case "mpe": $this->ctype="video/mpeg"; break;
		    case "mov": $this->ctype="video/quicktime"; break;
		    case "avi": $this->ctype="video/x-msvideo"; break;
			default:	$this->ctype="application/force-download";
		}
		return $this->ctype;
	}
	function ver_archivo($filename = ""){
		if($filename != ""){
			$this->filename = $filename;
		}
	    header("Content-Type: ".$this->get_ctype($this->filename));
		@readfile($this->galeria.$this->filename) or die("Error fatal; archivo no encontrado.<br>".$this->galeria.$this->filename);
		exit();
	}
	function start_download($filename = ""){
		if($filename != ""){
			$this->filename = $filename;
		}
		if($this->procesar()){
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public"); 
   header("Content-Description: File Transfer");
   header("Content-Type: ".$this->get_ctype($this->filename));
   header("Content-Disposition: attachment; filename=".$this->filename.";");
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$this->filesize);
	@readfile($this->galeria.$this->filename) or die("Error fatal; archivo no encontrado.");
			exit();
		}else{
			return false;
		}
	}
}
?>
Return current item: RapiDownload