File: D:/HostingSpaces/SBogers68/resortouddorpduin.nl/wwwroot/kms/lib/files/file_uploader.class.php
<?php
/**
* file_uploader.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 4/16/13
*/
class File_Uploader
{
/*
* File to upload
*/
private $_file;
/*
* Target where we will upload the file
*/
private $_uploadTarget;
/*
* Extensions that we are allowed to upload.
* Default set to jpg, jpeg, gif and png.
*/
private $_allowedExt = array('jpg','jpeg','gif','png');
/*
* Maximum size of a file that we upload.
* Default set to 10mb.
*/
private $_maxSize= 10000000;
/*
* Errors that occur during upload.
*/
private $_errors = array();
public function __construct($file)
{
$this->_file = $file;
$this->_uploadTarget = DOCUMENT_UPLOADS_ROOT;
}
/*
GET / SET
*/
/*
* Set
*/
public function set($prop,$val)
{
if( ! empty($prop))
{
$this->{$prop} = $val;
}
}
/*
* Set
*/
public function get($prop)
{
if( ! empty($prop))
{
return $this->{$prop};
}
return false;
}
/**
* Returns the extension of a given fileString
*
* @param string
* @return string
*/
public function getExt($input)
{
$temp = explode('.', $input);
$fileExt = strtolower($temp[count($temp) - 1]);
return $fileExt;
}
/*
* Convert bytes to megabytes
*/
private function mb($input)
{
return ceil($input / 1000000) . 'mb';
}
/*
IMAGE HANDLING
*/
/**
* Uploads the image
*
* @return boolean
*/
public function upload()
{
$path = $this->_uploadTarget.urlencode($this->_file['name']);
if(copy($this->_file['tmp_name'],$path))
{
return urlencode($this->_file['name']);
}
return FALSE;
}
/*
VALIDATION
*/
/**
* Validates the image
*
* @access public
* @return boolean
*/
public function validate()
{
if($this->checkSize() && $this->checkExt())
{
return TRUE;
}
return FALSE;
}
/**
* Checks if the file size is valid
*
* @return boolean
*/
private function checkSize()
{
if($this->_file['size'] < $this->_maxSize)
{
return TRUE;
}
// Set error message
$this->_errors[] = 'File size too big, this is ' . $this->mb($this->_file['size']) . ' and max is ' . $this->mb($this->_maxSize);
return FALSE;
}
/**
* Returns a string containing the extension of the file
*
* @return boolean
*/
private function checkExt()
{
$fileExt = $this->getExt($this->_file['name']);
if(in_array($fileExt, $this->_allowedExt))
{
return TRUE;
}
// Set error message
$msg = 'Invalid extension, '.$this->_file['name'].' has "'.$fileExt.'". Allowed are ';
foreach($this->_allowedExt as $ext)
{
$msg .= $ext.', ';
}
$msg = substr($msg,0,-2);
$this->_errors[] = $msg;
return FALSE;
}
/**
* Checks whether the upload directory is writable
*
* @access private
* @param
* @return boolean
*/
private function isWritable()
{
if(is_writable( $this->_uploadTarget ) )
{
return TRUE;
}
else
{
$this->_errors[] = 'Upload target "'.$this->_uploadTarget.'" is not writable';
return FALSE;
}
}
}