Improve the code, and add extention method

This commit is contained in:
Llewellyn 2024-09-12 03:01:07 +00:00
parent ba18089ccb
commit fc2bf847fe

View File

@ -1,12 +1,32 @@
/** /**
* Get the mime type based on file extension * Get the file extension from a full path OR file name.
* *
* @param string $file The file name or path * @param string $file The full file path or file name.
* *
* @return string the mime type on success * @return string The file extension in lowercase or an empty string if none found.
* * @since 5.0.3
*/ */
public static function mimeType($file) public static function extension(string $file): string
{
// Ensure the input is a valid string and contains a file extension
if (strpos($file, '.') === false)
{
return '';
}
// Extract and return the extension from the path
return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}
/**
* Get the mime type based on the full file path.
*
* @param string $file The full file path.
*
* @return string The mime type or 'application/octet-stream' if none found.
* @since 5.0.3
*/
public static function mimeType(string $file): string
{ {
/** /**
* **DISCLAIMER** * **DISCLAIMER**
@ -14,25 +34,60 @@
* array. It does not guarantee that the file is TRULY that * array. It does not guarantee that the file is TRULY that
* of the extension that this function returns. * of the extension that this function returns.
* https://gist.github.com/Llewellynvdm/74be373357e131b8775a7582c3de508b * https://gist.github.com/Llewellynvdm/74be373357e131b8775a7582c3de508b
*/ */
// get the extension form file // Ensure the file exists to avoid unnecessary processing
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (!is_file($file) || !is_readable($file))
// check if we have the extension listed {
if (isset(self::$fileExtensionToMimeType[$extension])) return self::mimeTypeFilename($file);
}
// Use finfo to get MIME type based on file content if available
if (function_exists('finfo_open'))
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file);
finfo_close($finfo);
if ($mimetype)
{
return $mimetype;
}
}
// Fallback to mime_content_type() if available
if (function_exists('mime_content_type'))
{
$mimetype = mime_content_type($file);
if ($mimetype)
{
return $mimetype;
}
}
// Fallback to application/octet-stream as a generic binary stream type
return 'application/octet-stream';
}
/**
* Get the mime type based on the file extension from a file name.
*
* @param string $file The file name (without path).
*
* @return string The mime type or 'application/octet-stream' if none found.
* @since 5.0.3
*/
protected static function mimeTypeFilename(string $file): string
{
// Get the extension
$extension = self::extension($file);
// Predefined MIME types by extension for quick lookup
if (!empty($extension) && isset(self::$fileExtensionToMimeType[$extension]))
{ {
return self::$fileExtensionToMimeType[$extension]; return self::$fileExtensionToMimeType[$extension];
} }
elseif (function_exists('mime_content_type'))
{ // Fallback to application/octet-stream if extension is unknown
return mime_content_type($file);
}
elseif (function_exists('finfo_open'))
{
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $file);
finfo_close($finfo);
return $mimetype;
}
return 'application/octet-stream'; return 'application/octet-stream';
} }