Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guessExtension() #1367

Closed
nowackipawel opened this issue Oct 27, 2018 · 1 comment
Closed

guessExtension() #1367

nowackipawel opened this issue Oct 27, 2018 · 1 comment

Comments

@nowackipawel
Copy link
Contributor

Am I wrong or this code does not have sense:

	public static function guessExtensionFromType(string $type) // $type contains MIME TYPE like 'image/jpeg'
	{
		$type = trim(strtolower($type), '. ');

		foreach (self::$mimes as $ext => $types)
		{
			if (is_string($types) && $types == $type)
			{
				return $ext;
			}
			else if (is_array($types) && in_array($type, $types))
			{
				return $ext;
			}
		}

		return null;
	}

for extensions like, i.e.:


	public static $mimes = [

		'mid'   => 'audio/midi',
		'midi'  => 'audio/midi',
		'mpga'  => 'audio/mpeg',
		'mp2'   => 'audio/mpeg',

		'jpg'   => ['image/jpeg', 'image/pjpeg'],
		'jpeg'  => ['image/jpeg', 'image/pjpeg'],
		'jpe'   => ['image/jpeg', 'image/pjpeg'],
		'jp2'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'j2k'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'jpf'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'jpg2'  => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'jpx'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'jpm'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'mj2'   => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
		'mjp2'  => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'],
];

for image/jpeg it always returns 'jpg' even if real file extension is jpeg or jpe which are also correct;

@lonnieezell
Copy link
Member

The purpose of that method is to attempt to reliably guess the file type based on the mime type of the file itself, not a user supplied input. In the case of this example - jpg, jpeg, and jpe are all the same file format and just different variations based on different common uses. But renaming it from jpeg to jpg will have no affect to the usability of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants