function mime_content_type($filename) { if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $filename); finfo_close($finfo); return $mime; } // define based on file extension $mime = mime_type_by_extension($filename); if ($mime) { return $mime; } else { return 'application/octet-stream'; } }
/** * Overridden to add stream context while uploading files with actual ContentType * @inheritdoc */ protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) { $file = new \stdClass(); $file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range); $file->size = strval($size); $file->type = $type; if ($this->validate($uploaded_file, $file, $error, $index)) { $this->handle_form_data($file, $index); $upload_dir = $this->get_upload_path(); if (!is_dir($upload_dir)) { mkdir($upload_dir, $this->options['mkdir_mode'], true); } $file_path = $this->get_upload_path($file->name); $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path); if ($uploaded_file && is_uploaded_file($uploaded_file)) { // multipart/formdata uploads (POST method uploads) file_put_contents($file_path, fopen($uploaded_file, 'r'), $append_file ? FILE_APPEND : 0, stream_context_create(array('s3' => array('ContentType' => mime_type_by_extension($name))))); } else { // Non-multipart uploads (PUT method support) file_put_contents($file_path, fopen($this->options['input_stream'], 'r'), $append_file ? FILE_APPEND : 0, stream_context_create(array('s3' => array('ContentType' => mime_content_type($uploaded_file))))); } $file_size = $this->get_file_size($file_path, $append_file); if ($file_size === $file->size) { $file->url = $this->get_download_url($file->name); if ($this->is_valid_image_file($file_path)) { $this->handle_image_file($file_path, $file); } } else { $file->size = $file_size; if (!$content_range && $this->options['discard_aborted_uploads']) { unlink($file_path); $file->error = $this->get_error_message('abort'); } } $this->set_additional_file_properties($file); } return $file; }
/** * Retrieve mime type of S3 object * @param string $fullPath * @return string */ public function getMimeType($fullPath) { $dynamicPath = $this->getDynamicPath($fullPath, false); $meta = $this->metadata($dynamicPath); $mime_type = $meta['content-type']; // try to define mime type based on file extension if default "octet-stream" is obtained if (end(explode('/', $mime_type)) === 'octet-stream') { $mime_type = mime_type_by_extension($this->get['path']); } return $mime_type; }