353 lines
10 KiB
PHP
353 lines
10 KiB
PHP
<?php
|
|
|
|
// author: Suri Bala
|
|
// freely distributable
|
|
// modified by Kusman Hioe (Artha Graha)
|
|
|
|
class FileUploadClass{
|
|
|
|
public $upload_tmp_dir = "/tmp/"; // leading and trailing slash required
|
|
public $file_upload_flag = "off";
|
|
private $upload_max_filesize_literal = "1.5M";
|
|
private $upload_max_filesize = 0;
|
|
public $allowable_upload_base_dirs = array("/tmp/");
|
|
public $allowable_upload_tmp_dirs = array( "/tmp/");
|
|
public $upload_dir= "/tmp/"; // leading and trailing slash required
|
|
public $upload_file_name;
|
|
public $display_error = 0;
|
|
public $new_filename = '';
|
|
//public $allowable_file_array = array('.html','.doc','.pdf','.xls','.jpg','.zip','.txt','.gif','.tar','.gz');
|
|
//public $allowable_file_array = array('.jpg','.gif','.png');
|
|
public $allowable_file_array = array('.pdf','.txt','.csv');
|
|
private $mime_type_allowed = array(
|
|
"application/pdf",
|
|
"text/csv",
|
|
"text/plain");
|
|
|
|
private $file_info;
|
|
|
|
public $last_msg;
|
|
public $ori_ext;
|
|
|
|
//for image
|
|
private $max_image_width_x = 576;
|
|
private $max_image_width_y = 384;
|
|
public $image_x;
|
|
public $image_y;
|
|
public $image_ratio_y = true;
|
|
public $image_ratio_x = false;
|
|
public $image_resize = true;
|
|
private $image_src_x;
|
|
private $image_src_y;
|
|
private $image_dst_x;
|
|
private $image_dst_y;
|
|
private $temp_src_file;
|
|
|
|
function __construct($name) {
|
|
|
|
if( is_null($_FILES[$name]) ) {
|
|
if ($this->debug) {
|
|
echo "Specified file <strong> ".$name." </strong> does not exist in the FILES array. Please check if it exists<br>\n";
|
|
echo "Exiting...";
|
|
}
|
|
exit;
|
|
|
|
}
|
|
|
|
/* if (file_exists($ini_file)) {
|
|
include $ini_file;
|
|
*/
|
|
$this->allowable_upload_base_dirs = array(UPLOADDIR,TEMPUPLOADDIR);
|
|
/* }
|
|
*/
|
|
$this->getConfigurationSettings();
|
|
if( $this->file_upload_flag == "off" ) {
|
|
if ($this->debug) {
|
|
echo "File upload capability in the configuration file is turned <strong> off </strong> . Please update the php.ini file.";
|
|
}
|
|
exit;
|
|
}
|
|
$this->upload_file_name = $name;
|
|
} // end of __construct
|
|
|
|
private function getConfigurationSettings() {
|
|
$max_filesize_server_setting = $this->convertFileSize(ini_get('upload_max_filesize'));
|
|
|
|
$this->file_upload_flag = ini_get('file_uploads');
|
|
$this->upload_tmp_dir = ini_get('upload_tmp_dir');
|
|
|
|
$max_filesize_prog_setting = $this->convertFileSize($this->upload_max_filesize_literal);
|
|
//echo "getConfigurationSettings = {$max_filesize_prog_setting} > {$max_filesize_server_setting}<br>";
|
|
if ($max_filesize_prog_setting > $max_filesize_server_setting) {
|
|
$this->upload_max_filesize = $max_filesize_server_setting;
|
|
} else {
|
|
$this->upload_max_filesize = $max_filesize_prog_setting;
|
|
}
|
|
}
|
|
|
|
private function convertFileSize($val){
|
|
$last = strtolower($val{strlen($val)-1});
|
|
switch($last) {
|
|
case 'g':
|
|
$val *= 1024;
|
|
case 'm':
|
|
$val *= 1024;
|
|
case 'k':
|
|
$val *= 1024;
|
|
}
|
|
return $val;
|
|
}
|
|
|
|
public function setFileSizeDefault(){
|
|
$val = ini_get('upload_max_filesize');
|
|
$this->upload_max_filesize = $this->convertFileSize($val);
|
|
}
|
|
|
|
public function getErrors() {
|
|
return $_FILES[$this->upload_file_name]['error'];
|
|
}
|
|
|
|
public function getFileSize() {
|
|
return $_FILES[$this->upload_file_name]['size'];
|
|
}
|
|
|
|
public function getFileName() {
|
|
if (!empty($this->new_filename)) {
|
|
$filename = $this->new_filename;
|
|
} else {
|
|
$filename = $_FILES[$this->upload_file_name]['name'];
|
|
}
|
|
return $filename;
|
|
}
|
|
|
|
public function getTmpName() {
|
|
return $_FILES[$this->upload_file_name]['tmp_name'];
|
|
}
|
|
|
|
public function setUploadDir($upload_dir) {
|
|
$success = 0;
|
|
trim($upload_dir);
|
|
|
|
if( $upload_dir[strlen($upload_dir)-1] != "/" ) {
|
|
$upload_dir .= "/"; // add trailing slash
|
|
}
|
|
$can_upload = false;
|
|
|
|
foreach( $this->allowable_upload_base_dirs as $dir ) {
|
|
|
|
if( $dir == $upload_dir ) {
|
|
$can_upload = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if( !$can_upload ) {
|
|
if ($this->debug) {
|
|
echo "Cannot upload to the dir ->".$upload_dir;
|
|
}
|
|
}else{
|
|
if ($this->debug) {
|
|
echo "Successfully setting upload dir ->".$upload_dir;
|
|
}
|
|
$this->upload_dir = $upload_dir;
|
|
$success = 1;
|
|
}
|
|
return $success;
|
|
} // end of setUploadDir
|
|
|
|
public function setTmpUploadDir($upload_tmp_dir) {
|
|
trim($upload_tmp_dir);
|
|
|
|
if( $upload_tmp_dir[strlen($upload_tmp_dir)-1] != "/" ) {
|
|
$upload_tmp_dir .= "/"; // add trailing slash
|
|
}
|
|
$can_upload = false;
|
|
|
|
foreach( $this->allowable_upload_base_dirs as $dir ) {
|
|
if( $dir == $upload_tmp_dir ) {
|
|
$can_upload = true;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if( !$can_upload ) {
|
|
if ($this->debug) {
|
|
if ($this->debug) {
|
|
echo "Cannot upload to the dir ->".$uplaod_tmp_dir;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
$this->upload_tmp_dir = $upload_dir;
|
|
}
|
|
|
|
public function uploadFile() {
|
|
$result = 0;
|
|
$this->temp_src_file = $this->upload_dir.'TEMP_'.$this->getFileName();
|
|
if (!move_uploaded_file($this->getTmpName(), $this->temp_src_file)){
|
|
$this->last_msg = "ERROR: Unable to copy file from temp directory.";
|
|
die($this->last_msg);
|
|
} else {
|
|
$this->info = getimagesize($this->temp_src_file);
|
|
|
|
if( $this->checkMaxMemorySizeLimit() ) {
|
|
if ($this->getFileSize() >0) {
|
|
$this->last_msg = "File size of ".number_format($this->getFileSize()/1024)." KB greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
|
|
if ($this->debug) {
|
|
echo $this->last_msg;
|
|
}
|
|
} else {
|
|
$this->last_msg = "File size is greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
|
|
}
|
|
} else {
|
|
if ($this->checkAllowableExtension()) {
|
|
switch($this->info['mime']) {
|
|
case 'image/jpeg':
|
|
$image_src = @imagecreatefromjpeg($this->temp_src_file);
|
|
break;
|
|
case 'image/gif':
|
|
$image_src = @imagecreatefromgif($this->temp_src_file);
|
|
break;
|
|
case 'image/png':
|
|
$image_src = @imagecreatefrompng($this->temp_src_file);
|
|
break;
|
|
default:
|
|
}
|
|
|
|
|
|
$this->image_src_x = imagesx($image_src);
|
|
$this->image_src_y = imagesy($image_src);
|
|
$this->image_dst_x = $this->image_src_x;
|
|
$this->image_dst_y = $this->image_src_y;
|
|
|
|
if ($this->image_resize) {
|
|
if ($this->image_ratio_x) {
|
|
if ($this->image_src_y > $this->image_y) {
|
|
$this->image_dst_y = $this->image_y;
|
|
$this->image_dst_x = round(($this->image_src_x * $this->image_y) / $this->image_src_y);
|
|
} else {
|
|
$this->image_dst_y = $this->image_src_y;
|
|
$this->image_dst_x = $this->image_src_x;
|
|
}
|
|
} else if ($this->image_ratio_y) {
|
|
if ($this->image_src_x > $this->image_x) {
|
|
$this->image_dst_y = round(($this->image_src_y * $this->image_x) / $this->image_src_x);
|
|
$this->image_dst_x = $this->image_x;
|
|
} else {
|
|
$this->image_dst_y = $this->image_src_y;
|
|
$this->image_dst_x = $this->image_src_x;
|
|
}
|
|
}
|
|
} else {
|
|
$this->image_dst_y = $this->image_src_y;
|
|
$this->image_dst_x = $this->image_src_x;
|
|
}
|
|
|
|
switch($this->info['mime']) {
|
|
case 'image/jpeg':
|
|
$image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
|
|
$res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
|
|
$result = @imagejpeg($image_dst,$this->temp_src_file);
|
|
|
|
break;
|
|
case 'image/gif':
|
|
$image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
|
|
$res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
|
|
$result = @imagegif($image_dst,$this->temp_src_file);
|
|
|
|
break;
|
|
case 'image/png':
|
|
$image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
|
|
$res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
|
|
$result = @imagepng($image_dst,$this->temp_src_file);
|
|
|
|
break;
|
|
default:
|
|
unlink($this->temp_src_file);
|
|
$result = 0;
|
|
$this->last_msg = "Mime type of file {$this->getFileName()} = {$this->info['mime']} is not allowed";
|
|
if ($this->debug) {
|
|
echo $this->last_msg;
|
|
}
|
|
}
|
|
|
|
if (is_resource($image_src)) imagedestroy($image_src);
|
|
if (is_resource($image_dst)) imagedestroy($image_dst);
|
|
|
|
|
|
// move the file from /tmp
|
|
if ($result) {
|
|
if( !rename($this->temp_src_file, $this->upload_dir.$this->getFileName()) ) {
|
|
$this->last_msg = "Failed to upload file ".$this->getTmpName();
|
|
if ($this->debug) {
|
|
echo "Failed to upload file ".$this->getTmpName();
|
|
}
|
|
} else {
|
|
$this->last_msg = "Successfully move file from " . $this->getTmpName(). " to " . $this->upload_dir.$this->getFileName();
|
|
if ($this->debug) {
|
|
echo "Successfully move file from " . $this->getTmpName(). " to " . $this->upload_dir.$this->getFileName();
|
|
}
|
|
}
|
|
} else {
|
|
$this->last_msg = "ERROR: Failed to upload gambar.";
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
$this->last_msg = "File ".$this->ori_ext . " is not allowed.";
|
|
if ($this->debug) {
|
|
echo "File ".$this->getTmpName() . " is not allowed.";
|
|
}
|
|
}
|
|
}
|
|
if (file_exists($this->temp_src_file)) unlink($this->temp_src_file);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function checkMaxMemorySizeLimit() {
|
|
/*echo "checkMaxMemorySizeLimit = {$this->getFileSize()} > {$this->upload_max_filesize}<br>";
|
|
print_r($_FILES);*/
|
|
if( $this->getFileSize() > $this->upload_max_filesize && $this->getFileSize() !== 0) {
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// Additional function
|
|
public function checkAllowableExtension() {
|
|
$filename = basename($this->getFileName());
|
|
preg_match_all('/(\.\w+)/i',$filename,$matches);
|
|
$this->ori_ext = strtolower(end($matches[1]));
|
|
if ((in_array($this->ori_ext,$this->allowable_file_array)) && (array_key_exists($this->info['mime'], array_flip($this->mime_type_allowed)))) {
|
|
$result = true;
|
|
} else {
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function checkFileExists(){
|
|
return file_exists($this->upload_dir.$this->getFileName());
|
|
}
|
|
|
|
public function setFilename($filename) {
|
|
$this->new_filename = $filename;
|
|
}
|
|
|
|
public function setMaxSize($x=null,$y=null){
|
|
if (!is_null($x)) {
|
|
$this->max_image_width_x = $x;
|
|
}
|
|
if (!is_null($y)) {
|
|
$this->max_image_width_y = $y;
|
|
}
|
|
|
|
}
|
|
|
|
} // end of class
|