Tema: Clase upload
Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/03/2010, 12:46
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 upload

Te dejo un ejemplo de una clase que uso:
Código PHP:
Ver original
  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | File Operations                                                                   |
  5. // +------------------------------------------------------------------------+
  6. //
  7.  
  8. require_once dirname(__FILE__) . '/Interface.php';
  9.  
  10. /**
  11.  * File Object
  12.  *
  13.  * @author  Christopher Valderrama <[email protected]>
  14.  */
  15. class File_Upload implements File_Interface
  16. {
  17.     /**
  18.      * The file info array
  19.      * @var array
  20.      */
  21.     private $_file;
  22.    
  23.     /**
  24.      * Creates a new File Object for processing upload files
  25.      *
  26.      * @param string $sFileName
  27.      */
  28.     public function __construct($sFileName)
  29.     {
  30.         if (!isset($_FILES)) {
  31.             throw new Exception('No File upload detected');
  32.         }
  33.         if (!isset($_FILES[$sFileName])) {
  34.             throw new Exception('File ' . $sFileName . ' not found in upload array');
  35.         }
  36.        
  37.         $this->_file = $_FILES[$sFileName];
  38.     }
  39.    
  40.     /**
  41.      * Returns the file name
  42.      *
  43.      * @return string
  44.      */
  45.     public function getName()
  46.     {
  47.         return $this->_file['name'];
  48.     }
  49.    
  50.     /**
  51.      * Returns the file type
  52.      *
  53.      * @return string
  54.      */
  55.     public function getType()
  56.     {
  57.         return $this->_file['type'];
  58.     }
  59.    
  60.     /**
  61.      * Returns the file size
  62.      *
  63.      * @return string
  64.      */
  65.     public function getSize()
  66.     {
  67.         return $this->_file['size'];
  68.     }
  69.    
  70.     /**
  71.      * Returns the file extension
  72.      *
  73.      * @return string
  74.      */
  75.     public function getExtension()
  76.     {
  77.         return strstr('.', $this->getName());
  78.     }
  79.    
  80.     /**
  81.      * Returns the Temporary file name and path
  82.      *
  83.      * @return string
  84.      */
  85.     public function getTemporaryName()
  86.     {
  87.         return $this->_file['tmp_name'];
  88.     }
  89.    
  90.     /**
  91.      * Returns the error code
  92.      *
  93.      * @return integer
  94.      */
  95.     public function getErrorCode()
  96.     {
  97.         return $this->_file['error'];
  98.     }
  99.    
  100.     /**
  101.      * Checks if the file is uploaded correctly
  102.      *
  103.      * @return bool
  104.      */
  105.     public function noErrors()
  106.     {
  107.         return ($this->getErrorCode() == UPLOAD_ERR_OK);
  108.     }
  109.    
  110.     /**
  111.      * Moves the file to a new location
  112.      *
  113.      * @param string $sDestination
  114.      * @param string $sNewFileName
  115.      * @return bool
  116.      */
  117.     public function moveFile($sDestination, $sNewFileName = '') {
  118.         if (!$this->isUploaded()) {
  119.             throw new Exception('Invalid upload file, error code:' . $this->getErrorCode());
  120.         }
  121.        
  122.         if (!is_dir($sDestination)) {
  123.             throw new Exception("Invalid Destination path: $sDestination");
  124.         }
  125.        
  126.         if (empty($sNewFileName)) {
  127.             $sFileName = $this->getName();
  128.         } else {
  129.             $sFileName = $sNewFileName;
  130.         }
  131.        
  132.         return move_uploaded_file($this->getTemporaryName(), $sDestination . '/' . $sFileName);
  133.     }
  134.    
  135.     /**
  136.      * Checks if a file isUploaded
  137.      *
  138.      * @return bool
  139.      */
  140.     public function isUploaded()
  141.     {
  142.         return ($this->noErrors() && is_uploaded_file($this->getTemporaryName()));
  143.     }
  144.    
  145.     /**
  146.      * Cleanup on destruction
  147.      * @return void
  148.      */
  149.     public function __destruct()
  150.     {
  151.         if (file_exists($this->getTemporaryName())) {
  152.             unlink($this->getTemporaryName());
  153.         }
  154.     }
  155. }

Interface:
Código PHP:
Ver original
  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | File Operations                                                                   |
  5. // +------------------------------------------------------------------------+
  6. //
  7.  
  8. /**
  9.  * File Interface
  10.  *
  11.  * @author  Christopher Valderrama <[email protected]>
  12.  */
  13. interface File_Interface
  14. {
  15.     /**
  16.      * Returns the Filename
  17.      * @return string
  18.      */
  19.     public function getName();
  20.     /**
  21.      * Returns the File type
  22.      * @return string
  23.      */
  24.     public function getType();
  25.     /**
  26.      * Returns the File size
  27.      * @return int
  28.      */
  29.     public function getSize();
  30.     /**
  31.      * Moves this file to a new place
  32.      * @param string $sDestination the path file destination
  33.      * @param string $sNewFileName optionally you can pass a new file name
  34.      * @return bool
  35.      */
  36.     public function moveFile($sDestination, $sNewFileName = '');
  37. }

La razón de tener una interface es que también tengo otra clase para trabajar con archivos locales, lo que me da flexibilidad en su uso.

Para usarla hago algo así:
Código PHP:
Ver original
  1. $File = new File_Upload('picture');
  2. $File->moveFile('./uploads');
  3. $sFileName = $File->getName();
  4. $nSize = $File->getSize();

Saludos.

Última edición por GatorV; 24/03/2010 a las 12:51