<?php
/**
* FileUpload - To handle single file upload
*
* @author Jason Lam <hide@address.com>
* @version 1.0.0
* @access public
* @copyright Released under the LGPL License, see COPYING file
*
*/
class FileUpload {
/**
* _userfile
*
* @var file itself
* @access private
*/
var $_userfile;
/**
* _userfile_size
*
* @var file size
* @access private
*/
var $_userfile_size;
/**
* _userfile_name
*
* @var file name
* @access private
*/
var $_userfile_name;
/**
* _destDir
*
* @var Where the file gets upload to on server
* @access private
*/
var $_destDir;
/**
* _tempDir
*
* @var Where the file is held temporaily on server before being
* moved to _destDir
* @access private
*/
var $_tempDir;
/**
* _maxFileSize
*
* @var Maximum Size of the uploaded file. Note php.ini file may need
* Need adjusting for max file size
* @access private
*/
var $_maxFileSize = 3072000; // 3Megs
/**
* _maxFileSize
*
* @var Maximum Size the Directory can be
* @access private
*/
var $_maxDirSize = 5120000; // 5Megs
/**
* _ext
*
* @var NON Allowable Extensions
* Default set in filemanager.class.php
* @access private
*/
var $_ext = array();
/**
* !Constructor
*
* @param userfile, userfile size, userfile name, destiniation dir, temp dir
* @return none
* @access public
*/
function FileUpload($userfile,$userfile_size,$userfile_name,$destDir,$tempDir) {
$this->_userfile = $userfile;
$this->_userfile_size = $userfile_size;
$this->_userfile_name = $userfile_name;
$this->_destDir = $destDir;
$this->_tempDir = $tempDir;
}
/**
* Validate File type against ext array
*
* @param none
* @return boolean
* @access public
*/
function validateFileType() {
$result = true;
$lenExt = count($this->_ext);
for ($i=0; $i<$lenExt; $i++) {
if (stristr($this->_userfile_name,$this->_ext[$i])) {
$result = false; // set to return validation fail if match found
break;
}
}
return $result;
}
/**
* Validate File Size, see private variable $_maxFileSize
*
* @param none
* @return boolean
* @access public
*/
function validateFileSize() {
if ($this->_userfile_size > $this->_maxFileSize)
return false;
else
return true;
}
/**
* Validate Directory Size, see private variable $_maxDirSize
*
* @param none
* @return boolean
* @access public
*/
function validateDirSize() {
if (($this->getDirSize()+$this->_userfile_size) > $this->_maxDirSize) {
return false;
} else {
return true;
}
}
/**
* Upload File, does not validation, you need call the appropriate
* validation methods if validation is required. Note if a file
* with the same filename the existing file will be replaced with
* the new file
*
* @param none
* @return boolean
* @access public
*/
function upload() {
if (file_exists("$this->_tempDir/$this->_userfile_name")) {
unlink("$this->_tempDir/$this->_userfile_name");
}
rename("$this->_userfile", "$this->_tempDir/$this->_userfile_name");
copy("$this->_tempDir/$this->_userfile_name", "$this->_destDir/$this->_userfile_name");
}
/**
* Get the current size of the directory
*
* @param none
* @return boolean
* @access public
*/
function getDirSize() {
$total=0;
$handle=opendir($this->_destDir);
while ($file = readdir($handle)) {
if (is_file("$this->_destDir/$file")) {$total+=filesize("$this->_destDir/$file");}
}
return $total;
}
// Setters and Getters
function getFileName() { return $this->_userfile_name; }
function getFileSize() { return $this->_userfile_size; }
function getFileBinaryData() { return $this->_userfile; }
function getDestDir() { return $this->_destDir; }
function getTempDir() { return $this->_tempDir; }
function setMaxFileSize($maxFileSize) { $this->_maxFileSize = $maxFileSize; }
function setMaxDirSize($maxDirSize) {$this->_maxDirSize = $maxDirSize; }
function setValidExt($ext) { $this->_ext = $ext; }
}
?>