Add throw exception to get mime info if there is no mime type found
Instead of returning empty
This commit is contained in:
@@ -38,6 +38,11 @@ print "GETFILENAMEENDING: $file: " . File::getFilenameEnding($file) . "<br>";
|
||||
$file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php';
|
||||
print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "<br>";
|
||||
print "MIMEINFO: $file: " . File::getMimeType($file) . "<br>";
|
||||
try {
|
||||
print "MIMEINFO: $file: " . File::getMimeType("does_not_exists.txt") . "<br>";
|
||||
} catch (\Exception $e) {
|
||||
print "Error: " . get_class($e) . ": " . $e->getMessage() . "<br>";
|
||||
}
|
||||
|
||||
print "</body></html>";
|
||||
|
||||
|
||||
@@ -58,15 +58,21 @@ class File
|
||||
* else returns '' for any other finfo read problem
|
||||
*
|
||||
* @param string $read_file File to read, relative or absolute path
|
||||
* @return string
|
||||
* @return string mime type
|
||||
* @throws \UnexpectedValueException if file cannot be read or is not a file
|
||||
* @throws \RangeException if we cannot get a mime type and throw exception is on
|
||||
*/
|
||||
public static function getMimeType(string $read_file): string
|
||||
{
|
||||
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||
if (!is_file($read_file)) {
|
||||
if (!is_file($read_file) || !is_readable($read_file)) {
|
||||
throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file);
|
||||
}
|
||||
return $finfo->file($read_file) ?: '';
|
||||
$mime_type = $finfo->file($read_file);
|
||||
if ($mime_type === false) {
|
||||
throw new \RangeException('[getMimeType] Cannot get mime type for: ' . $read_file);
|
||||
}
|
||||
return $mime_type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user