allowable_ext = '*'; return; } if (is_array($array_allowable_ext)) { $this->allowable_ext = $array_allowable_ext; } else { echo 'setAllowableFormat method error : The argument for must be an array'; return; } } /** * Method to set maximum size of file to upload * * @param integer $int_max_size * @return void */ public function setMaxSize($int_max_size) { // checking for php.ini upload_size $this->max_size = intval($int_max_size); } /** * Method to set upload file directory * * @param string $str_upload_dir * @return void */ public function setUploadDir($str_upload_dir) { $this->upload_dir = $str_upload_dir; } /** * Method to upload file * * @param string $file_input_name * @param string $str_new_filename * @return integer */ public function doUpload($file_input_name, $str_new_filename = '') { // get file extension $file_ext = substr($_FILES[$file_input_name]['name'], strrpos($_FILES[$file_input_name]['name'], '.')); if (empty($str_new_filename)) { $this->new_filename = basename($_FILES[$file_input_name]['name']); } else { $this->new_filename = $str_new_filename.$file_ext; } $_isTypeAllowed = 0; // checking file extensions if ($this->allowable_ext != '*') { foreach ($this->allowable_ext as $ext) { if ($ext == $file_ext) { $_isTypeAllowed++; } } if (!$_isTypeAllowed) { $this->error = 'Filetype is forbidden'; return FILETYPE_NOT_ALLOWED; } } // check for file size $_size_kb = ((integer)$this->max_size)/1024; if ($_FILES[$file_input_name]['size'] > $this->max_size) { $this->error = 'Filesize is excedded maximum uploaded file size'; return FILESIZE_EXCED; } // uploading file if (self::chunkUpload($_FILES[$file_input_name]['tmp_name'], $this->upload_dir.'/'.$this->new_filename)) { return UPLOAD_SUCCESS; } else { $upload_error = error_get_last(); $error_msg = ''; if ($upload_error) { $error_msg = 'PHP Error ('.$upload_error['message'].')'; } $this->error = 'Upload failed. Upload directory is not writable or not exists. '.$error_msg; return UPLOAD_FAILED; } } public function chunkUpload($tmpfile,$target_file){ set_time_limit(0); $orig_file_size = filesize($tmpfile); $chunk_size = 256; // chunk in bytes $upload_start = 0; $handle = fopen($tmpfile, "rb"); $fp = fopen($target_file, 'w'); while($upload_start < $orig_file_size) { $contents = fread($handle, $chunk_size); fwrite($fp, $contents); if($upload_start % 10000 == 0){ $count = array('data'=>array('upload_progress' => ceil(($upload_start/$orig_file_size)*100).'%')); } if (ENVIRONMENT === 'development') { echo ''; } $upload_start += strlen($contents); fseek($handle, $upload_start); } fclose($handle); fclose($fp); unlink($tmpfile); return true; } }