Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/08/2014, 10:46
Avatar de miguec04
miguec04
 
Fecha de Ingreso: agosto-2008
Ubicación: Cimitarra, Santander
Mensajes: 378
Antigüedad: 15 años, 8 meses
Puntos: 15
Información Clase Util File Upload PHP

Hola amigos, quiero compartir esta clase que hice para el cargue de los archivos en php, lo hago por medio de esta clase debido a que si lo deseo puedo agregar 4 archivos los monto en session y no tengo la necesidad de guardarlo hasta que opriman el botón guardar que seria cuando ya sabrían o estarían seguro que lo desean hacer, con esto evito subir archivos basura al server.

Agradezco que me den sus opiniones.

Código PHP:
Ver original
  1. namespace modelo\lib;
  2.  
  3. use modelo\CUtil;
  4. use modelo\data\ADto;
  5. use modelo\Lang;
  6.  
  7. class Files extends ADto {
  8.    
  9.     private $_file;
  10.     private $type;
  11.     private $size;
  12.     private $folder;
  13.     private $name;
  14.     private $temp;
  15.     private $extensions;
  16.     private $formats;
  17.    
  18.     public function __construct() {
  19.         $this->formats          = array('txt' => 'text/plain',
  20.                                         'htm' => 'text/html',
  21.                                         'html' => 'text/html',
  22.                                         'php' => 'text/html',
  23.                                         'css' => 'text/css',
  24.                                         'js' => 'application/javascript',
  25.                                         'json' => 'application/json',
  26.                                         'xml' => 'application/xml',
  27.                                         'swf' => 'application/x-shockwave-flash',
  28.                                         'flv' => 'video/x-flv',
  29.                        
  30.                                         // images
  31.                                         'png' => 'image/png',
  32.                                         'jpg' => 'image/jpeg',
  33.                                         'jpeg' => 'image/jpeg',
  34.                                         'jpg' => 'image/jpeg',
  35.                                         'gif' => 'image/gif',
  36.                                         'bmp' => 'image/bmp',
  37.                                         'ico' => 'image/vnd.microsoft.icon',
  38.                                         'tiff' => 'image/tiff',
  39.                                         'tif' => 'image/tiff',
  40.                                         'svg' => 'image/svg+xml',
  41.                                         'svgz' => 'image/svg+xml',
  42.                        
  43.                                         // archives
  44.                                         'zip' => 'application/zip',
  45.                                         'rar' => 'application/x-rar-compressed',
  46.                                                            
  47.                                         // audio/video
  48.                                         'mp3' => 'audio/mpeg',
  49.                                         'mp3' => 'audio/mp3',
  50.                                         'qt' => 'video/quicktime',
  51.                                         'mov' => 'video/quicktime',
  52.                        
  53.                                         // adobe
  54.                                         'pdf' => 'application/pdf',
  55.                                         'psd' => 'image/vnd.adobe.photoshop',
  56.                                         'ai' => 'application/postscript',
  57.                                         'eps' => 'application/postscript',
  58.                                         'ps' => 'application/postscript',
  59.                        
  60.                                         // ms office
  61.                                         'doc' => 'application/msword',
  62.                                         'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  63.                                         'rtf' => 'application/rtf',
  64.                                         'xls' => 'application/vnd.ms-excel',
  65.                                         'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  66.                                         'ppt' => 'application/vnd.ms-powerpoint',
  67.  
  68.                                         // open office
  69.                                         'odt' => 'application/vnd.oasis.opendocument.text',
  70.                                         'ods' => 'application/vnd.oasis.opendocument.spreadsheet',);
  71.         parent::__construct();
  72.     }
  73.    
  74.     public function addUpload($_file) {
  75.         $this->name         = $_file["name"];
  76.         $this->size         = $_file["size"];
  77.         $this->type         = $_file["type"];
  78.         $this->temp         = $_file["tmp_name"];
  79.         $this->_file        = file_get_contents($_file["tmp_name"]);
  80.         foreach($this->formats as $key => $f) {
  81.             if($f==$this->type) {
  82.                 $this->extensions   = $key;
  83.                 break;
  84.             }
  85.         }
  86.         if(CUtil::getVacio($this->extensions)) {
  87.             Messages::addError(NULL,'La extensión que desea ingresar no es soportada');
  88.             return FALSE;
  89.         }
  90.         return TRUE;
  91.     }
  92.    
  93.     public function getHeaderWrite() {
  94.         header('Content-Description: '.$this->type);
  95.         header('Content-Type: application/octet-stream');
  96.         header('Content-Disposition: attachment; filename=' . $this->folder.$this->getNameShort().'.'.$this->extensions);
  97.         header('Content-Transfer-Encoding: binary');
  98.         header('Expires: 0');
  99.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  100.         header('Pragma: public');
  101.         header('Accept-Ranges: bytes');
  102.         header('Content-Length: ' .$this->size);
  103.         header('Connection: close');
  104.         ob_clean();
  105.         flush();
  106.         echo $this->getContent();
  107.         exit;
  108.     }
  109.    
  110.     protected function getNameShort() {
  111.         $partes     = explode('.',$this->name);
  112.         unset($partes[CUtil::getCountArray($partes)-1]);
  113.         return implode('',$partes);
  114.     }
  115.    
  116.     public function getWriteFile() {
  117.         $fileNew    = fopen($this->folder.$this->name, "w");
  118.         fwrite($fileNew,$this->getContent());
  119.         fclose($fileNew);
  120.     }
  121.    
  122.     protected function getHeader() {
  123.         header("Content-type:".$this->type);
  124.         echo $this->getContent();
  125.     }
  126.    
  127.     public function getContent() {
  128.         return $this->_file;
  129.     }
  130.    
  131.     /**
  132.      * @tutorial Metodo Descripcion:
  133.      * @version 1.0
  134.      * @author Miguel Carmona 9/08/2014
  135.      * @return the $_file
  136.      * @param field_type
  137.      */
  138.     public final function getFile() {
  139.         return $this->_file;
  140.     }
  141.  
  142.     /**
  143.      * @tutorial Metodo Descripcion:
  144.      * @version 1.0
  145.      * @author Miguel Carmona 9/08/2014
  146.      * @return the $type
  147.      * @param field_type
  148.      */
  149.     public final function getType() {
  150.         return $this->type;
  151.     }
  152.  
  153.     /**
  154.      * @tutorial Metodo Descripcion:
  155.      * @version 1.0
  156.      * @author Miguel Carmona 9/08/2014
  157.      * @return the $size
  158.      * @param field_type
  159.      */
  160.     public final function getSize() {
  161.         return $this->size;
  162.     }
  163.  
  164.     /**
  165.      * @tutorial Metodo Descripcion:
  166.      * @version 1.0
  167.      * @author Miguel Carmona 9/08/2014
  168.      * @return the $folder
  169.      * @param field_type
  170.      */
  171.     public final function getFolder() {
  172.         return $this->folder;
  173.     }
  174.  
  175.     /**
  176.      * @tutorial Metodo Descripcion:
  177.      * @version 1.0
  178.      * @author Miguel Carmona 9/08/2014
  179.      * @return the $name
  180.      * @param field_type
  181.      */
  182.     public final function getName() {
  183.         return $this->name;
  184.     }
  185.  
  186.     /**
  187.      * @tutorial Metodo Descripcion:
  188.      * @version 1.0
  189.      * @author Miguel Carmona 9/08/2014
  190.      * @return the $formats
  191.      * @param multitype:string
  192.      */
  193.     public final function getFormats() {
  194.         return $this->formats;
  195.     }
  196.  
  197.     /**
  198.      * @tutorial Metodo Descripcion:
  199.      * @version 1.0
  200.      * @author Miguel Carmona 9/08/2014
  201.      * @return the $_file
  202.      * @param field_type
  203.      */
  204.     public final function setFile($_file) {
  205.         $this->_file = $_file;
  206.     }
  207.  
  208.     /**
  209.      * @tutorial Metodo Descripcion:
  210.      * @version 1.0
  211.      * @author Miguel Carmona 9/08/2014
  212.      * @return the $type
  213.      * @param field_type
  214.      */
  215.     public final function setType($type) {
  216.         $this->type = $type;
  217.     }
  218.  
  219.     /**
  220.      * @tutorial Metodo Descripcion:
  221.      * @version 1.0
  222.      * @author Miguel Carmona 9/08/2014
  223.      * @return the $size
  224.      * @param field_type
  225.      */
  226.     public final function setSize($size) {
  227.         $this->size = $size;
  228.     }
  229.  
  230.     /**
  231.      * @tutorial Metodo Descripcion:
  232.      * @version 1.0
  233.      * @author Miguel Carmona 9/08/2014
  234.      * @return the $folder
  235.      * @param field_type
  236.      */
  237.     public final function setFolder($folder) {
  238.         $this->folder = $folder;
  239.     }
  240.  
  241.     /**
  242.      * @tutorial Metodo Descripcion:
  243.      * @version 1.0
  244.      * @author Miguel Carmona 9/08/2014
  245.      * @return the $name
  246.      * @param field_type
  247.      */
  248.     public final function setName($name) {
  249.         $this->name = $name;
  250.     }
  251.  
  252.     /**
  253.      * @tutorial Metodo Descripcion:
  254.      * @version 1.0
  255.      * @author Miguel Carmona 9/08/2014
  256.      * @return the $formats
  257.      * @param multitype:string
  258.      */
  259.     public final function setFormats($formats) {
  260.         $this->formats = $formats;
  261.     }
  262. }
__________________
Desoftc Technology - Miguel Carmona
Creaciones Inteligentes - Cimitarra Colombia
[email protected]
http://www.desoftc.com.co