Te dejo un ejemplo de una clase que uso:
Código PHP:
Ver original<?php
//
// +------------------------------------------------------------------------+
// | File Operations |
// +------------------------------------------------------------------------+
//
require_once dirname(__FILE__) . '/Interface.php';
/**
* File Object
*
*/
class File_Upload implements File_Interface
{
/**
* The file info array
* @var array
*/
private $_file;
/**
* Creates a new File Object for processing upload files
*
* @param string $sFileName
*/
public function __construct($sFileName)
{
throw new Exception('No File upload detected');
}
if (!isset($_FILES[$sFileName])) { throw new Exception('File ' . $sFileName . ' not found in upload array');
}
$this->_file = $_FILES[$sFileName];
}
/**
* Returns the file name
*
* @return string
*/
public function getName()
{
return $this->_file['name'];
}
/**
* Returns the file type
*
* @return string
*/
{
return $this->_file['type'];
}
/**
* Returns the file size
*
* @return string
*/
public function getSize()
{
return $this->_file['size'];
}
/**
* Returns the file extension
*
* @return string
*/
public function getExtension()
{
return strstr('.', $this->getName()); }
/**
* Returns the Temporary file name and path
*
* @return string
*/
public function getTemporaryName()
{
return $this->_file['tmp_name'];
}
/**
* Returns the error code
*
* @return integer
*/
public function getErrorCode()
{
return $this->_file['error'];
}
/**
* Checks if the file is uploaded correctly
*
* @return bool
*/
public function noErrors()
{
return ($this->getErrorCode() == UPLOAD_ERR_OK);
}
/**
* Moves the file to a new location
*
* @param string $sDestination
* @param string $sNewFileName
* @return bool
*/
public function moveFile($sDestination, $sNewFileName = '') {
if (!$this->isUploaded()) {
throw new Exception('Invalid upload file, error code:' . $this->getErrorCode());
}
throw new Exception("Invalid Destination path: $sDestination");
}
if (empty($sNewFileName)) { $sFileName = $this->getName();
} else {
$sFileName = $sNewFileName;
}
}
/**
* Checks if a file isUploaded
*
* @return bool
*/
public function isUploaded()
{
}
/**
* Cleanup on destruction
* @return void
*/
public function __destruct()
{
unlink($this->getTemporaryName()); }
}
}
Interface:
Código PHP:
Ver original<?php
//
// +------------------------------------------------------------------------+
// | File Operations |
// +------------------------------------------------------------------------+
//
/**
* File Interface
*
*/
interface File_Interface
{
/**
* Returns the Filename
* @return string
*/
public function getName();
/**
* Returns the File type
* @return string
*/
/**
* Returns the File size
* @return int
*/
public function getSize();
/**
* Moves this file to a new place
* @param string $sDestination the path file destination
* @param string $sNewFileName optionally you can pass a new file name
* @return bool
*/
public function moveFile($sDestination, $sNewFileName = '');
}
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$File = new File_Upload('picture');
$File->moveFile('./uploads');
$sFileName = $File->getName();
$nSize = $File->getSize();
Saludos.