/** * ฟังก์ชั่นตรวจสอบไฟล์อัปโหลดว่าเป็นรูปภาพหรือไม่ * * @param array $excepts ชนิดของไฟล์ที่ยอมรับเช่น array('jpg', 'gif', 'png') * @param array $file_upload รับค่ามาจาก $_FILES * @return array|bool คืนค่าแอเรย์ [width, height, mime] ของรูปภาพ หรือ false ถ้าไม่ใช่รูปภาพ */ public static function isImage($excepts, $file_upload) { // ext $imageinfo = explode('.', $file_upload['name']); $imageinfo = array('ext' => strtolower(end($imageinfo))); if (in_array($imageinfo['ext'], $excepts)) { // Exif $info = getImageSize($file_upload['tmp_name']); if ($info[0] == 0 || $info[1] == 0 || !Mime::check($excepts, $info['mime'])) { return false; } else { $imageinfo['width'] = $info[0]; $imageinfo['height'] = $info[1]; $imageinfo['mime'] = $info['mime']; return $imageinfo; } } else { return false; } }
/** * Upload file * * @param string $name * @param array $options * @return string|boolean */ public function upload() { if (!isset($_FILES[$this->name])) { return FALSE; } $file = $_FILES[$this->name]; $cogear = getInstance(); event('file.preupload', $file); switch ($file['error']) { case UPLOAD_ERR_CANT_WRITE: $this->errors[] = t('Can\'t upload file. Check write permission for temporary folder.', 'File Errors'); break; case UPLOAD_ERR_INI_SIZE: $this->errors[] = t('File size is bigger that it\'s allowed in <b>php.ini</b> (%s).', 'File Errors', ini_get('upload_max_filesize')); break; case UPLOAD_ERR_NO_FILE: $this->isRequired && ($this->errors[] = t('You didn\'t choose file to upload.', 'File Errors')); break; case UPLOAD_ERR_PARTIAL: $this->errors[] = t('Please, upload file once again.', 'File Errors'); break; case UPLOAD_ERR_NO_TMP_DIR: $this->errors[] = t('Temporary directory is not corrected.', 'File Errors'); break; } if ($file['error'] == UPLOAD_ERR_OK) { if ($this->options->allowed_types) { $types = is_string($this->options->allowed_types) ? new Core_ArrayObject(preg_split('#[^a-z]#', $this->options->allowed_types, -1, PREG_SPLIT_NO_EMPTY)) : $this->options->allowed_types; $ext = pathinfo($file['name'], PATHINFO_EXTENSION); $result = FALSE; foreach ($types as $type) { $type == $ext && ($result = TRUE); } !$result && ($this->errors[] = t('Only following types of files are allowed: <b>%s</b>.', 'File Errors', $types->toString('</b>, <b>'))); } $result = Mime::check($file['name'], $file['type']); if ($result !== TRUE) { $this->errors[] = t('File you are trying to upload has unusual MIME-type. It is like <b>%s</b>, but it was expected to be <b>%s</b>', 'File Errors', $file['type'], $result); } $this->options->maxsize && $this->checkMaxSize($file['size'], $this->options->maxsize); if (!$this->options->path) { $this->errors[] = t('Upload path is not defined.', 'File Erros'); } strpos($this->options->path, ROOT) !== FALSE or $this->options->path = UPLOADS . DS . $this->options->path; Filesystem::makeDir($this->options->path); if (!is_dir($this->options->path)) { $this->errors[] = t('Upload path <b>%s</b> doesn\'t exist.', 'File Errors', $this->options->path); } $file['name'] = $this->prepareFileName($file['name']); $file['path'] = $this->options->path . DS . $file['name']; $this->file = new Core_ArrayObject($file); return !$this->errors ? $this->processUpload() : FALSE; } return NULL; }
/** * Generated from @assert (array('jpg','gif','png'), 'image/png') [==] true. * * @covers Mime::check */ public function testCheck() { $this->assertTrue(\Mime::check(array('jpg', 'gif', 'png'), 'image/png')); }