any comments or suggestions is highly appreciated..thanks all...
Code: Select all
<?php
class Upload
{
public $path, $field, $allowed_types, $filename, $max_size;
protected $error_messages = array();
/**
* Initialize the parameter for file upload
*
* @param array $config
*
*/
public function __construct($config = array())
{
foreach($config as $conf => $value)
{
$this->$conf = $value;
}
}
/**
* Execute The Uploading Of File
* @access public
*/
public function uploadFile()
{
$file = pathinfo($this->getUploadedFileName());
//get file name
$filename = $file['filename'] = $this->filename;
//get file type
$type = $file['extension'];
//get file path
$file_path = $this->path . '/';
//get all file name, type and path
$file_path = $file_path . $filename . "." . $type;
//check if the file browser has a file selected
if($this->getUploadedFileName() != null)
{
//validate if the type is known in the array
if($this->isAllowedTypes($type))
{
if(is_executable($file_path))
{
$this->error_messages[] = "This File is Not Allowed to upload Due To Some Security Reason!";
}
else
{
//check if the the document file size is not reach the maximum file size
if($this->getUploadedFileSize() <= $this->max_size)
{
//upload document if no errors occur
if(move_uploaded_file($this->getUploadedTemporaryName(), $file_path))
{
echo "The file ". basename($this->getUploadedFileName()). " has been uploaded";
}
}
else
{
$this->error_messages[] = "You Have Reached The Maximum File Size Limit!";
}
}
}
else
{
$this->error_messages[] = "Unknown File Type";
}
}
else
{
$this->error_messages[] = "No File Selected";
}
}
/**
* Validate If Uploading File Has an Errors
*
* @return bool
*/
public function isUploaded()
{
$count_error = count($this->displayErrorMessages());
$return = true;
if($count_error > 0)
{
$return = false;
}
return $return;
}
private function isExecutable()
{
$return = true;
if (is_executable($this->getUploadedFileName()))
{
$return = false;
}
return $return;
}
/**
* Get Upload Path
*
* @access public
* @return string
*
*/
public function getFilePath()
{
return $this->path;
}
/**
* Get File Name of the file being uploaded
* @access public
* @return string
*/
public function getFileName()
{
return $this->filename;
}
/**
* Filter all allowed file types
*
* @param string $file_extension
* @return bool
*/
public function isAllowedTypes($file_extension)
{
$this->allowed_types = explode('|', $this->allowed_types);
if(is_array($this->allowed_types))
{
$return = false;
if(in_array(strtolower($file_extension), $this->allowed_types))
{
$return = true;
}
return $return;
}
}
/**
* Get Document File Size
*
* @return int
*/
private function getUploadedFileSize()
{
return $_FILES[$this->field]['size'];
}
/**
* Get Document Name
*
* @return string
*/
private function getUploadedFileName()
{
return $_FILES[$this->field]['name'];
}
/**
* Get Temporary Document Name
*
* @return string
*/
private function getUploadedTemporaryName()
{
return $_FILES[$this->field]['tmp_name'];
}
/**
* Get error messages
*
* @return array
*/
private function getErrorMessages()
{
return $this->error_messages;
}
/**
* Display error message depends on the type of error
*
* @return string
*/
public function displayErrorMessages()
{
foreach($this->getErrorMessages() as $error)
{
$str .= $error;
$str .= "<br>";
}
return $str;
}
}
//call form class
include 'form.php';
$form = new Form;
if($_POST['submit'])
{
//call upload class
$config['path'] = 'test';
$config['field'] = 'uploaded_file';
$config['filename'] = 'tolits';
$config['allowed_types'] = 'jpg|jpeg|gif|png|doc|pdf|html|exe';
$config['max_size'] = 350000;
$uploader = new Upload($config);
/* $uploader->uploadFile();
echo $uploader->displayErrorMessages();*/
$uploader->uploadFile();
if (!$uploader->isUploaded())
{
echo $uploader->displayErrorMessages();
}
}
//begin form
echo $form->startMultipartForm('form_upload', 'upload_file.php', 'post');
//echo '<form action=upload_file.php method=post>';
echo $form->fileBrowser('uploaded_file');
echo '<input type=submit name=submit value=submit>';
//end form
$form->endForm();
?>