Php mime type from extension

Get Mime-Type by file extension in PHP

Recently I build a PHP script for dynamically downloading files, and I came across the need to provide the right Mime-Type for each file I set to download. I wrote a function in PHP that gets file extension (with or without the dot prefix) and returns the correct Mime-Type.

This function also returns the general purpose mime-type for missing or unknown file types.

General purpose MIME-TYPE

Use: application / octet — stream

case ‘.arc’ : $mime = ‘application/octet-stream’ ; break ; // Archive document (multiple files embedded)

case ‘.docx’ : $mime = ‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’ ; break ; // Microsoft Word (OpenXML)

case ‘.js’ : $mime = ‘application/javascript’ ; break ; // JavaScript (IANA Specification) (RFC 4329 Section 8.2)

case ‘.mid’ : $mime = ‘audio/midi audio/x-midi’ ; break ; // Musical Instrument Digital Interface (MIDI)

Читайте также:  This reserved word java

case ‘.midi’ : $mime = ‘audio/midi audio/x-midi’ ; break ; // Musical Instrument Digital Interface (MIDI)

case ‘.odp’ : $mime = ‘application/vnd.oasis.opendocument.presentation’ ; break ; // OpenDocument presentation document

case ‘.ods’ : $mime = ‘application/vnd.oasis.opendocument.spreadsheet’ ; break ; // OpenDocument spreadsheet document

case ‘.odt’ : $mime = ‘application/vnd.oasis.opendocument.text’ ; break ; // OpenDocument text document

case ‘.pptx’ : $mime = ‘application/vnd.openxmlformats-officedocument.presentationml.presentation’ ; break ; // Microsoft PowerPoint (OpenXML)

case ‘.swf’ : $mime = ‘application/x-shockwave-flash’ ; break ; // Small web format (SWF) or Adobe Flash document

case ‘.xlsx’ : $mime = ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’ ; break ; // Microsoft Excel (OpenXML)

1 Comment

maya.kimhi

Leave a comment Cancel reply

You must be logged in to post a comment.

cute amateur anal and big cum facial in porn debut most beautiful pussy in the world gay my hero academia sex big ass trany porn clips

High definition xxx video of Vira doing anal Horny amateur teen peeing and masturbating on cam

This website uses cookies to improve your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

josantonius / php-mime-type Public archive

PHP library to get MIME types from extensions

License

josantonius/php-mime-type

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

PHP library to get MIME types from extensions.

The original concept of my project, linking a single mimetype to a file extension, has been found to be insufficient in accurately reflecting the many-to-one relationships that exist in real-world use-cases. In practice, multiple file extensions often correspond to a single mimetype, rendering my existing design approach suboptimal.

Given these insights, I’ve decided to archive this repository. I recommend those interested to look at the approach taken by the repository patrickmccallum/mimetype-io. This project adopts a more nuanced perspective, effectively managing mimetypes by acknowledging the possibility of multiple extensions corresponding to a single mimetype. I believe this is a more promising approach for managing mimetypes moving forward.

The preferred way to install this extension is through Composer.

To install PHP MimeType library, simply:

composer require josantonius/mime-type

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

composer require josantonius/mime-type --prefer-source

You can also clone the complete repository with Git:

git clone https://github.com/josantonius/php-mime-type.git

Get array with all MIME types:

public function all(): array;

Get file extension from MIME type:

public function getExtension(string $mimeType): string|null;

Get MIME type from file extension:

public function getMime(string $extension): string|null;

Get array with all MIME types:

public static function all(): array;

Get file extension from MIME type:

public static function getExtension(string $mimeType): string|null;

Get MIME type from file extension:

public static function getMime(string $extension): string|null;

Example of use for this library:

Get array with all MIME types

use Josantonius\MimeType\MimeType; $mimeType = new MimeType(); $mimeType->all();
use Josantonius\MimeType\MimeTypeCollection; MimeTypeCollection::all();
[ [".123"]=> "application/vnd.lotus-1-2-3" [".3dml"]=> "text/vnd.in3d.3dml" [".3g2"]=> "video/3gpp2" [".3gp"]=> "video/3gpp" [".7z"]=> "application/x-7z-compressed" [".aab"]=> "application/x-authorware-bin" [".aac"]=> "audio/x-aac" [".aam"]=> "application/x-authorware-map" [".aas"]=> "application/x-authorware-seg" [".abw"]=> "application/x-abiword" (. ) ]

Get file extension from MIME type

use Josantonius\MimeType\MimeType; $mimeType = new MimeType(); $mimeType->getExtension('text/html'); // .html
use Josantonius\MimeType\MimeTypeCollection; MimeTypeCollection::getExtension('application/zip'); // .zip

Get MIME type from file extension

use Josantonius\MimeType\MimeType; $mimeType = new MimeType(); $mimeType->getMime('.tar'); // application/x-tar // The dot can be omitted: $mimeType->getMime('mp4'); // video/mp4
use Josantonius\MimeType\MimeTypeCollection; MimeTypeCollection::getMime('.json'); // application/json

To run tests you just need composer and to execute the following:

git clone https://github.com/josantonius/php-mime-type.git

Run code standard tests with PHPCS:

Run PHP Mess Detector tests to detect inconsistencies in code style:

  • Add new feature
  • Improve tests
  • Improve documentation
  • Improve English translation in the README file
  • Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)

Detailed changes for each release are documented in the release notes.

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.

If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊

This repository is licensed under the MIT License.

About

PHP library to get MIME types from extensions

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP package for converting file extensions to MIME types and vice versa.

License

ralouphie/mimey

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

readme.md

PHP package for converting file extensions to MIME types and vice versa.

This package uses httpd’s mime.types to generate a mapping of file extension to MIME type and the other way around.

The mime.types file is parsed by bin/generate.php and converted into an optimized PHP array in mime.types.php which is then wrapped by helper class \Mimey\MimeTypes .

$mimes = new \Mimey\MimeTypes; // Convert extension to MIME type: $mimes->getMimeType('json'); // application/json // Convert MIME type to extension: $mimes->getExtension('application/json'); // json

It’s rare, but some extensions have multiple MIME types:

// Get all MIME types for an extension: $mimes->getAllMimeTypes('wmz'); // array('application/x-ms-wmz', 'application/x-msmetafile')

However, there are many MIME types that have multiple extensions:

// Get all extensions for a MIME type: $mimes->getAllExtensions('image/jpeg'); // array('jpeg', 'jpg', 'jpe')

You can add custom conversions by changing the mapping that is given to MimeTypes .

There is a MimeMappingBuilder that can help with this:

// Create a builder using the built-in conversions as the basis. $builder = \Mimey\MimeMappingBuilder::create(); // Add a conversion. This conversion will take precedence over existing ones. $builder->add('custom/mime-type', 'myextension'); $mimes = new \Mimey\MimeTypes($builder->getMapping()); $mimes->getMimeType('myextension'); // custom/mime-type $mimes->getExtension('custom/mime-type'); // myextension

You can add as many conversions as you would like to the builder:

$builder->add('custom/mime-type', 'myextension'); $builder->add('foo/bar', 'foobar'); $builder->add('foo/bar', 'fbar'); $builder->add('baz/qux', 'qux'); $builder->add('cat/qux', 'qux'); ...

Optimized Custom Conversion Loading

You can optimize the loading of custom conversions by saving all conversions to a compiled PHP file as part of a build step.

// Add a bunch of custom conversions. $builder->add(. ); $builder->add(. ); $builder->add(. ); ... // Save the conversions to a cached file. $builder->save($cache_file_path);

The file can then be loaded to avoid overhead of repeated $builder->add(. ) calls:

// Load the conversions from a cached file. $builder = \Mimey\MimeMappingBuilder::load($cache_file_path); $mimes = new \Mimey\MimeTypes($builder->getMapping());
composer require ralouphie/mimey 

About

PHP package for converting file extensions to MIME types and vice versa.

Источник

Оцените статью