Created
September 1, 2021 12:59
-
-
Save pavarov/40f5b14ca92ce780d2321c3c751e01a8 to your computer and use it in GitHub Desktop.
Wrapper over SplFileInfo. added getMimeContentType, getMimeType methods. Notice! throws a FIleNotFoundException (you should have this class).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\helpers; | |
use app\exceptions\FileNotFoundException; | |
use SplFileInfo; | |
/** | |
* Class FileInfo | |
* | |
* Wrapper over SplFileInfo | |
*/ | |
class FileInfo extends SplFileInfo | |
{ | |
/** | |
* @throws FileNotFoundException | |
*/ | |
public function __construct($filename) | |
{ | |
parent::__construct($filename); | |
if (!$this->isFile()) { | |
throw new FileNotFoundException($filename . ' не является файлом или не найден'); | |
} | |
} | |
public function getMimeContentType() | |
{ | |
return mime_content_type($this->getPathname()); | |
} | |
public function getMimeType() | |
{ | |
$extension = $this->getExtension(); | |
$mimeType = null; | |
if (!empty($extension)) { | |
switch ($extension) { | |
case 'txt': | |
$mimeType = 'text/plain'; | |
break; | |
case 'odt': | |
$mimeType = 'application/vnd.oasis.opendocument.text'; | |
break; | |
case 'doc': | |
$mimeType = 'application/msword'; | |
break; | |
case 'docx': | |
$mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; | |
break; | |
case 'jpg': | |
case 'jpeg': | |
$mimeType = 'image/jpeg'; | |
break; | |
case 'png': | |
$mimeType = 'image/png'; | |
break; | |
case 'pdf': | |
$mimeType = 'application/pdf'; | |
break; | |
case 'zip': | |
$mimeType = 'application/zip'; | |
break; | |
case 'xlsx': | |
$mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; | |
break; | |
case 'xls': | |
$mimeType = 'application/vnd.ms-excel'; | |
break; | |
default: | |
break; | |
} | |
} | |
return $mimeType ?: $this->getMimeContentType(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment