debug) { echo "Specified file ".$name." does not exist in the FILES array. Please check if it exists
\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 off . 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}
"; 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}
"; 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