Tuesday, March 23, 2010

PHP file upload class

/**
* File uploader class
* @name FileUploader
* @author Ritesh
*/
class FileUploader
{
/**
* Constant variables
*/
const ERR_INVALID_FILE = 'Invalid file';
const ERR_FILE_SIZE = 'File size is too large';
const ERR_UNKNOWN = 'Unknown file upload error';
const ERR_FILE_EXISTS = 'File already exist';


/**
* Default 100 KB
* @var string $_filesize
*/
private $_filesize = "102400";

/**
*
* @var array $_file_types
*/
private $_file_types = array('image/jpeg','image/ico','image/gif','image/png');

/**
* detault root folder of class file
* @var string $_upload_path
*/
private $_upload_path = ".";

/**
* error message
* @var string $_error
*/
private $_error = '';

/**
* @var string $_uploaded_file
*/
private $_uploaded_file = '';

/**
* Add other files
* @param array $file_types
*/
function addFileTypes($file_types)
{
$this->_file_types = array_merge($this->_file_types,$file_types);
}

/**
* reset upload file types
* @param array $file_types
*/
function resetFileTypes($file_types)
{
$this->_file_types = $file_types;
}

/**
* set file size
* @param int $size
*/
function setFileSize($size)
{
$this->_filesize = $size;
}

/**
* set file upload path
* @param string $path
*/
function setUploadPath($path)
{
$this->_upload_path = $path;
}

/**
* set error message
* @param string $error
*/
function setError($error)
{
$this->_error = $error;
}

/**
* return error message
* @return string
*/
function getError()
{
return __($this->_error);
}

/**
* File upload function to upload file
* @param string $input_name
* @param boolean $check_file_exists
* @param string $saved_file_name
* @return boolean
*/
function upload( $input_name, $check_file_exists = false, $saved_file_name = null )
{
$file_type = $_FILES[$input_name]['type'];
$file_size = $_FILES[$input_name]['size'];

if($saved_file_name == null)
$saved_file_name = $_FILES[$input_name]['name'];
else
$saved_file_name = $saved_file_name.".".$this->getFileExt($_FILES[$input_name]['name']);

# Check file type
if (!in_array($file_type, $this->_file_types))
{
$this->setError(self::ERR_INVALID_FILE);
return false;
}

# Check file size
if ($file_size > $this->_filesize)
{
$this->setError(self::ERR_FILE_SIZE);
return false;
}

# Check for unknown error
if ($_FILES[$input_name]["error"] > 0)
{
$this->setError(self::ERR_UNKNOWN);
return false;
}

# Check file exist
if ($check_file_exists && file_exists($this->_upload_path . DS . $saved_file_name))
{
$this->setError(self::ERR_FILE_EXISTS);
return false;
}

$result = move_uploaded_file($_FILES[$input_name]["tmp_name"],$this->_upload_path . DS . $saved_file_name);

if (!$result)
{
$this->setError(self::ERR_UNKNOWN);
$this->_uploaded_file = '';
return false;
}
else
{
$this->_uploaded_file = $saved_file_name;
}

return $result;
}

function getUploadedFile()
{
return $this->_uploaded_file;
}

/**
* get file extention
* @param string $file_name
*/
private function getFileExt($file_name)
{
$filename = strtolower($file_name) ;
$exts = split("[/\\.]", $file_name) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}


}

?>

No comments:

Post a Comment