Skip to content

Instantly share code, notes, and snippets.

@pavarov
Created September 1, 2021 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavarov/40f5b14ca92ce780d2321c3c751e01a8 to your computer and use it in GitHub Desktop.
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).
<?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