KumbiaPHP beta2-dev
Framework PHP en español
upload.php
Ir a la documentación de este archivo.
00001 <?php
00022 abstract class Upload
00023 {
00029         protected $_name;
00030 
00036         protected $_allowScripts = FALSE;
00037         
00043         protected $_minSize = NULL;
00044 
00050         protected $_maxSize = NULL;
00051 
00057         protected $_types = NULL;
00058         
00064         protected $_extensions = NULL;
00065         
00071         protected $_overwrite = FALSE;
00072 
00078         public function __construct($name)
00079         {
00080                 $this->_name = $name;
00081         }
00082         
00088         public function setAllowScripts($value)
00089         {
00090                 $this->_allowScripts = $value;
00091         }
00092         
00098         public function setMinSize($size)
00099         {
00100                 $this->_minSize = trim($size);
00101         }
00102 
00108         public function setMaxSize($size)
00109         {
00110                 $this->_maxSize = trim($size);
00111         }
00112         
00118         public function setTypes($value)
00119         {
00120                 if(! is_array($value)) $value = explode('|', $value);
00121                 $this->_types = $value;
00122         }
00123         
00129         public function setExtensions($value)
00130         {
00131                 if(! is_array($value)) $value = explode('|', $value);
00132                 $this->_extensions = $value;
00133         }
00134         
00140         public function overwrite($value)
00141         {
00142                 $this->_overwrite = (bool) $value;
00143         }
00144 
00151         protected function _beforeSave($name) 
00152         {}
00153 
00159         protected function _afterSave($name) 
00160         {}
00161         
00168         public function save($name = NULL)
00169         {
00170                 if(!$this->isUploaded()) {
00171                         return FALSE;
00172                 }       
00173                 if(!$name) {
00174                         $name = $_FILES[$this->_name]['name'];
00175                 } else {
00176                         $name = $name . $this->_getExtension();
00177                 }
00178                 // Guarda el archivo
00179                 if($this->_beforeSave($name) !== FALSE && $this->_overwrite($name) && $this->_validates() && $this->_saveFile($name))   {
00180                         $this->_afterSave($name);
00181                         return TRUE;
00182                 }
00183         }
00184         
00190         public function saveRandom()
00191         {
00192                 // Genera el nombre de archivo
00193                 $name = md5(time());
00194                 
00195                 // Guarda el archivo
00196                 if($this->save($name)) {
00197                         return $name . $this->_getExtension();
00198                 }
00199                 
00200                 return FALSE;
00201         }
00202         
00208         public function isUploaded()
00209         {
00210                 // Verifica si ha ocurrido un error al subir            
00211                 if ($_FILES[$this->_name]['error'] > 0) {
00212                         $error = array(
00213                                 UPLOAD_ERR_INI_SIZE => 'el archivo excede el tamaño máximo ('.ini_get('upload_max_filesize').'b) permitido por el servidor',
00214                                 UPLOAD_ERR_FORM_SIZE => 'el archivo excede el tamaño máximo permitido',
00215                                 UPLOAD_ERR_PARTIAL => 'se ha subido el archivo parcialmente',
00216                                 UPLOAD_ERR_NO_FILE => 'no se ha subido ningún archivo',     
00217                                 UPLOAD_ERR_NO_TMP_DIR => 'no se encuentra el directorio de archivos temporales',
00218                                 UPLOAD_ERR_CANT_WRITE => 'falló al escribir el archivo en disco',
00219                                 UPLOAD_ERR_EXTENSION => 'una extensión de php ha detenido la subida del archivo'
00220                         );
00221         
00222                 Flash::error('Error: ' . $error[$_FILES[$this->_name]['error']]);
00223                 return FALSE;
00224                 }               
00225         return TRUE;
00226         }
00227         
00233         protected function _validates()
00234         {
00235                 // Denegar subir archivos de scripts ejecutables
00236                 if(!$this->_allowScripts && preg_match('/\.(php|phtml|php3|php4|js|shtml|pl|py|rb|rhtml)$/i', $_FILES[$this->_name]['name'])) {
00237                         Flash::error('Error: no esta permitido subir scripts ejecutables');
00238                         return FALSE;
00239                 }
00240                 
00241                 // Valida el tipo de archivo
00242                 if($this->_types !== NULL && !$this->_validatesTypes()) {
00243                         Flash::error('Error: el tipo de archivo no es válido');
00244                         return FALSE;
00245                 }
00246                 
00247                 // Valida extension del archivo
00248                 if($this->_extensions !== NULL && !preg_match('/\.(' . implode('|', $this->_extensions) . ')$/i', $_FILES[$this->_name]['name'])) {
00249                         Flash::error('Error: la extensión del archivo no es válida');
00250             return FALSE;
00251                 }
00252                 
00253                 // Verifica si es superior al tamaño indicado
00254         if($this->_maxSize !== NULL && $_FILES[$this->_name]['size'] > $this->_toBytes($this->_maxSize)) {
00255             Flash::error("Error: no se admiten archivos superiores a $this->_maxSize".'b');
00256             return FALSE;
00257         }
00258                 
00259                 // Verifica si es inferior al tamaño indicado
00260         if($this->_minSize !== NULL && $_FILES[$this->_name]['size'] < $this->_toBytes($this->_minSize)) {
00261             Flash::error("Error: no se admiten archivos inferiores a $this->_minSize".'b');
00262             return FALSE;
00263         }
00264                 
00265                 return TRUE;
00266         }
00267         
00273         protected function _validatesTypes()
00274         {
00275                 return in_array($_FILES[$this->_name]['type'], $this->_types);
00276         }
00277         
00283         protected function _getExtension()
00284         {
00285                 if($ext = explode('.',$_FILES[$this->_name]['name'])){
00286                         $ext = '.'. end($ext);
00287                 } else {
00288                          $ext = NULL;
00289                 }
00290                 
00291                 return $ext;
00292         }
00293         
00299         protected function _overwrite($name)
00300         {
00301                 if($this->_overwrite){
00302                         return TRUE;
00303                 }
00304                 if(file_exists("$this->_path/$name")){
00305                         Flash::error('Error: ya existe este fichero. Y no se permite reescribirlo');
00306                         return FALSE;
00307                 }
00308                 return TRUE;
00309         }
00310 
00317         protected function _toBytes($size)
00318         {
00319                 if(is_int($size) || ctype_digit($size)){
00320                         return (int) $size;
00321                 }               
00322                 
00323                 $tipo = strtolower(substr($size, -1));
00324                 $size = (int) $size;
00325 
00326                 switch($tipo) {
00327                         case 'g': //Gigabytes
00328                                 $size *= 1073741824;
00329                                 break;
00330                         case 'm': //Megabytes
00331                                 $size *= 1048576;
00332                                 break;
00333                         case 'k': //Kilobytes
00334                                 $size *= 1024;
00335                                 break;
00336                         default :
00337                                 $size = -1;
00338                                 Flash::error('Error: el tamaño debe ser un int para bytes, o un string terminado con K, M o G. Ej: 30k , 2M, 2G');
00339                 }               
00340                 
00341                 return $size;
00342         }
00343         
00350         protected abstract function _saveFile($name);
00351         
00359         public static function factory($name, $adapter = 'file')
00360         {
00361                 require_once dirname (__FILE__)."/adapters/{$adapter}_upload.php";
00362                 $class = $adapter.'upload';
00363                 
00364                 return new $class($name);
00365         }
00366 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones