| @@ -17,24 +17,15 @@ | ||
| 17 | 17 | |
| 18 | 18 | private $files = array(); |
| 19 | 19 | private $helper; |
| 20 | 20 | |
| 21 | - /** | |
| 22 | - * On init | |
| 23 | - * Assign $_FILES array to $files | |
| 24 | - */ | |
| 21 | + // construct | |
| 25 | 22 | public function __construct() { |
| 26 | 23 | $this->helper = Helper_E2pdf_Helper::instance(); |
| 27 | 24 | $this->files = $_FILES; |
| 28 | 25 | } |
| 29 | 26 | |
| 30 | - /** | |
| 31 | - * Get value from $_FILES | |
| 32 | - * | |
| 33 | - * @param string $key - Array key | |
| 34 | - * | |
| 35 | - * @return mixed | |
| 36 | - */ | |
| 27 | + // get | |
| 37 | 28 | public function get($key) { |
| 38 | 29 | |
| 39 | 30 | if (!$key) { |
| 40 | 31 | if (!empty($this->files)) { |
| @@ -51,8 +42,9 @@ | ||
| 51 | 42 | } |
| 52 | 43 | } |
| 53 | 44 | } |
| 54 | 45 | |
| 46 | + // get upload max filesize | |
| 55 | 47 | public function get_upload_max_filesize() { |
| 56 | 48 | $max_size = -1; |
| 57 | 49 | if ($max_size < 0) { |
| 58 | 50 | $post_max_size = $this->helper->load('convert')->to_bytes(ini_get('post_max_size')); |
| @@ -67,5 +59,35 @@ | ||
| 67 | 59 | } |
| 68 | 60 | return $this->helper->load('convert')->from_bytes($max_size); |
| 69 | 61 | } |
| 70 | 62 | |
| 63 | + // upload | |
| 64 | + public function upload($file, $exts = [], $mimes = []) { | |
| 65 | + $upload = [ | |
| 66 | + 'name' => $file['name'], | |
| 67 | + 'tmp_name' => $file['tmp_name'], | |
| 68 | + 'type' => $file['type'], | |
| 69 | + 'ext' => strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)), | |
| 70 | + ]; | |
| 71 | + if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { | |
| 72 | + $upload['error'] = __('Choose file to upload', 'e2pdf'); | |
| 73 | + } elseif (!empty($file['error'])) { | |
| 74 | + $upload['error'] = $file['error']; | |
| 75 | + } elseif (!is_readable($file['tmp_name'])) { | |
| 76 | + $upload['error'] = __('TMP file is not readable', 'e2pdf'); | |
| 77 | + } elseif (!empty($exts) && !in_array($upload['ext'], $exts)) { | |
| 78 | + $upload['error'] = sprintf(__('Only %s files allowed', 'e2pdf'), '.' . implode(', .', $exts)); | |
| 79 | + } elseif (!empty($mimes) && function_exists('finfo_open') && function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) { | |
| 80 | + $finfo = finfo_open(FILEINFO_MIME_TYPE); | |
| 81 | + if ($finfo !== false) { | |
| 82 | + $mime = @finfo_file($finfo, $file['tmp_name']); | |
| 83 | + finfo_close($finfo); | |
| 84 | + if ($mime && !in_array($mime, $mimes)) { | |
| 85 | + $upload['error'] = __('Invalid Type', 'e2pdf'); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + } elseif (!empty($mimes) && !in_array($upload['type'], $mimes)) { | |
| 89 | + $upload['error'] = __('Invalid Type', 'e2pdf'); | |
| 90 | + } | |
| 91 | + return $upload; | |
| 92 | + } | |
| 71 | 93 | } |