Ver Mensaje Individual
  #4 (permalink)  
Antiguo 11/09/2012, 12:54
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Clase para subir archivos al servidor.

No entiendo realmente para que usar esa clase, se me hace más complicada que usar el array $_FILES junto con move_uploaded_file, es lo más sencillo, si quieres checar la extensión es trivial también, puedes usar una función así:
Código PHP:
Ver original
  1. class Upload
  2. {
  3.     private $destDir;
  4.     private $checkExtensions;
  5.     private $extensions;
  6.     private $errors = array();
  7.     private $upload_errors = array(
  8.         UPLOAD_ERR_INI_SIZE    => "Larger than upload_max_filesize.",
  9.         UPLOAD_ERR_FORM_SIZE    => "Larger than form MAX_FILE_SIZE.",
  10.         UPLOAD_ERR_PARTIAL    => "Partial upload.",
  11.         UPLOAD_ERR_NO_FILE        => "No file.",
  12.         UPLOAD_ERR_NO_TMP_DIR    => "No temporary directory.",
  13.         UPLOAD_ERR_CANT_WRITE    => "Can't write to disk.",
  14.         UPLOAD_ERR_EXTENSION     => "File upload stopped by extension.",
  15.         UPLOAD_ERR_EMPTY        => "File is empty." // add this to avoid an offset
  16.     );
  17.    
  18.     public function __construct($destDir, $checkExt = false, array $extensions = array())
  19.     {
  20.         $this->destDir = $destDir;
  21.         $this->checkExtensions = $checkExt;
  22.         $this->extensions = $extensions;
  23.     }
  24.    
  25.     public function setExtensions(array $extensions)
  26.     {
  27.         $this->extensions = $extensions;
  28.        
  29.         return $this;
  30.     }
  31.    
  32.     public function getExtensions()
  33.     {
  34.         return $this->extensions;
  35.     }
  36.    
  37.     public function setCheckExtensions($checkExt = true)
  38.     {
  39.         $this->checkExtensions = $checkExt;
  40.        
  41.         return $this;
  42.     }
  43.    
  44.     public function getCheckExtensions()
  45.     {
  46.         return $this->checkExtensions;
  47.     }
  48.    
  49.     public function getErrorsFor($name)
  50.     {
  51.         return $this->errors[$name];
  52.     }
  53.    
  54.     public function getErrors()
  55.     {
  56.         return $this->errors;
  57.     }
  58.    
  59.     public function addError($name, $error) {
  60.         if (!isset($this->errors[$name])) $this->errors[$name] = array();
  61.        
  62.         $this->errors[$name][] = $error;
  63.        
  64.         return $this;
  65.     }
  66.    
  67.     public function moveFile($name)
  68.     {
  69.         if (!$this->isValid($name)) {
  70.             return false;
  71.         }
  72.        
  73.         if (!move_uploaded_file($_FILES[$name]['tmp_name'], $destDir)) {
  74.             throw new RuntimeException("can't move file to destination dir: {$destDir}");
  75.         }
  76.        
  77.         return true;
  78.     }
  79.    
  80.     public function getName($name)
  81.     {
  82.         if (!$this->isValid($name)) {
  83.             return false;
  84.         }
  85.        
  86.         return $_FILES[$name]['name'];
  87.     }
  88.    
  89.     public function getExtension($name)
  90.     {
  91.         if (!$this->isValid($name)) {
  92.             return false;
  93.         }
  94.        
  95.         $fileInfo = pathinfo($_FILES[$name]['name']);
  96.         $ext = strtolower($fileInfo['extension']);
  97.        
  98.         return $ext;
  99.     }
  100.    
  101.     private function isValid($name)
  102.     {
  103.         if (!isset($_FILES[$name])) {
  104.             $this->addError($name, "missing in \$_FILES");
  105.             return false;
  106.         }
  107.    
  108.         if ($_FILES[$name]['error'] != UPLOAD_ERR_OK) {
  109.             $translated = $this->translateUploadError($_FILES[$name]['error']);
  110.             $this->addError($name, $translated);
  111.             return false;
  112.         }
  113.    
  114.         if (!is_uploaded_file($_FILES[$name]['tmp_name'])) {
  115.             $this->addError($name, 'file wasn\'t uploaded by POST, possible attack');
  116.             return false;
  117.         }
  118.    
  119.         if ($checkExt) {
  120.             $ext = $this->getExtension($name);
  121.        
  122.             if (!in_array($ext, $this->extensions)) {
  123.                 $this->addError($name, "invalid extension: {$ext}");
  124.                 return false;
  125.             }
  126.         }
  127.        
  128.         return true;
  129.     }
  130.    
  131.     private function translateUploadError($error)
  132.     {
  133.         return $this->upload_errors[$error];
  134.     }
  135. }

Uso:
Código PHP:
Ver original
  1. $upload = new Upload('./uploads/', true, array('jpg', 'gif'));
  2. if ($upload->moveFile('file')) {
  3.     $name = $upload->getName('file');
  4.     echo "{$name} uploaded correctly";
  5. } else {
  6.     foreach ($upload->getErrorsFor('file') as $error) {
  7.         echo $error;
  8.     }
  9. }