<?php
//+-----------------------------------------------+//
//+ Copyright Brian Shawa
//+ fileupload.php
//+ Description: A class for handling file uploads
//+ Dependencies: NONE
//+
//+ LAST UPDATED : 26 June 2009
//+-----------------------------------------------+//
class FileUpload {
//Debug and error control
var $obj_status = false; // error control (externally)
var $debug_data = array(); // Debug (object generated)
var $debug_data_vars; // Debug (environment vars)
var $error_count = 0; //error control (internally)
//General vars
public $message;
public $saved_file_name;
public $saved_file_path;
public $filesize_actual;
public $filetype_actual;
public $filetype_actuals;
public $file_extension;
public $file_upload_name;
var $global_debug_mode = 0;
var $lang_array = array('select_file' => 'Please select file to upload',
'file_too_big' => 'File size error',
'type_not_allowed' => 'File type is not allowed',
'chmod_error' => 'Unable to write file to server');
function FileUpload($new_filename,$destination_folder,$filesize_limit,$allowed_extensions,$allowed_file_types) {
GLOBAL $config; // >>STAND ALONE USAGE <<, set this in calling file
$this->filetype_actuals = 'apples';
//___General Settings______________________________
$this->global_debug_mode = $config['debug_mode']; // >>STAND ALONE USAGE <<, set this in calling file
//Sanity Check
if($new_filename == '' || $destination_folder == '' || $filesize_limit == '' || $allowed_extensions == '' || $allowed_file_types == '') {
$this->obj_status = false;
} else {
//___remove trailing slash____________________________
if (substr($destination_folder, -1) == '/'){
$destination_folder = substr($destination_folder, 0, -1);
}
//___get file attributes______________________________
$filename = $_FILES['fileupload']['name'];
$file_content = $_FILES['fileupload']['tmp_name'];
$filesize_actual = $_FILES['fileupload']['size'];
$filetype_actual = strtolower($_FILES['fileupload']['type']);
$this->filesize_actual = $filesize_actual;
$this->filetype_actual = $filetype_actual;
$this->file_upload_name = $filename;
//____check file size_________________________________
if($filesize_actual > $filesize_limit || $filesize_actual <= 0) {
$this->error_count += 1;
$this->message = $this->lang_array['file_too_big'];
}
//____check file type_________________________________
if(!in_array($filetype_actual,$allowed_file_types)) {
$this->error_count += 1;
$this->message = $this->lang_array['type_not_allowed'];
}
//____check file extension_____________________________
$file_extension = strtolower(end(explode('.',$filename)));
$this->file_extension = $file_extension;
if(!in_array($file_extension,$allowed_extensions) || $file_extension == '') {
$this->error_count += 1;
$this->message = $this->lang_array['type_not_allowed'];
}
//____check destination folder_____________________________
if(!is_writeable($destination_folder)) {
$this->error_count += 1;
$this->message = $this->lang_array['chmod_error'];
}
//____save file to disk - set true___________________________________
$filename_random = $destination_folder.'/'.$new_filename.'.'.$file_extension;
if($this->error_count == 0){
if(!move_uploaded_file($_FILES['fileupload']['tmp_name'],$filename_random)) {
$this->obj_status = false;
$this->message = 'Error - could not save the file..try again';
}
}
//_____sanity check___________________________________________________
if(is_file($filename_random)){
$this->saved_file_path = $filename_random;
$this->saved_file_name = $new_filename.'.'.$file_extension;
$this->obj_status = true;
}
//______________Run Debug______________________________________
$this->debug_data_vars = get_defined_vars();
$this->PrintDebug();
}
}
//______________Debug Method______________________________________
private function PrintDebug() {
if($this->global_debug_mode == 1) {
echo '<b>Debug Message: </b>'.$this->message;
echo '<br />';
echo '<b>Debug Data: </b>';
//print out all vars available
foreach($this->debug_data_vars as $key => $value){
echo '<br />';
echo "$key => $value";
echo '<br />';
}
echo 'saved_file_name: '.$this->saved_file_name; //e.g. output : GtejdFts.gif
echo '<br />';
echo 'saved_file_size: '.$this->filesize_actual; // e.g. output : 12332
echo '<br />';
echo 'saved_file_path: '.$this->saved_file_name; //e.g. output : /var/home/file/Negsyhs.png
echo '<br />';
echo 'original_filename: '.$this->file_upload_name; //e.g. output : my_cat.jpg
echo '<br />';
} //debug mode
}
//===================================================================== USAGE =================================================================
/*
NOTES:
------
1) Uploaded file extension is maintained.
2) HTML - Form filed name for file upload should be "fileupload"
3) Remember to add enctype="multipart/form-data" in your HTML form
From with calling file
-----------------------
//set allowed file types
$allowed_extensions = array('gif',
'jpg',
'bmp',
'png',
'jpeg');
$allowed_file_types = array('image/gif',
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/x-png',
'image/bmp',
'image/png');
$new_filename = RandomCode(7);
$filesize_limit = 1000000;
$destination_folder = '/home/live160/public_html/content/images'; //with or without trailing /
//upload file
$fileupload = new FileUpload($new_filename, $destination_folder, $filesize_limit, $allowed_extensions, $allowed_file_types);
//check for upload errors
if(!$fileupload->obj_status){
$display_notification = $fileupload->message; //error message
}else{
// Carry on
}
//Additional:
$saved_file_name = $fileupload->saved_file_name; //e.g. output : GtejdFts.gif
$saved_file_size = $fileupload->filesize_actual; // e.g. output : 12332
$saved_file_path = $fileupload->saved_file_name; //e.g. output : /var/home/file/Negsyhs.png
$original_filename = $fileupload->file_upload_name; //e.g. output : my_cat.jpg
$
*/
}
?>