{"id":15022833,"url":"https://github.com/softcreatr/php-mime-detector","last_synced_at":"2025-08-11T13:09:00.825Z","repository":{"id":45953036,"uuid":"153057502","full_name":"SoftCreatR/php-mime-detector","owner":"SoftCreatR","description":"Detect a file's mime type using magic numbers.","archived":false,"fork":false,"pushed_at":"2024-09-01T17:21:55.000Z","size":101,"stargazers_count":24,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-11T13:07:17.007Z","etag":null,"topics":["composer","detection","extension","file","file-extension","file-type","filetype","magic-numbers","mime","mime-type","mimetype","php","php5","php53","php54","php55","php56","php7","php8","phpunit"],"latest_commit_sha":null,"homepage":"https://1-2.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SoftCreatR.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"softcreatr","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-10-15T05:26:24.000Z","updated_at":"2024-10-04T03:04:11.000Z","dependencies_parsed_at":"2024-09-22T09:00:37.939Z","dependency_job_id":"7e13592f-d068-49cc-859f-f86c2cda06cd","html_url":"https://github.com/SoftCreatR/php-mime-detector","commit_stats":{"total_commits":43,"total_committers":4,"mean_commits":10.75,"dds":"0.16279069767441856","last_synced_commit":"e7b4e27269804a47998c80def56520a35fdcdb59"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/SoftCreatR/php-mime-detector","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCreatR%2Fphp-mime-detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCreatR%2Fphp-mime-detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCreatR%2Fphp-mime-detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCreatR%2Fphp-mime-detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftCreatR","download_url":"https://codeload.github.com/SoftCreatR/php-mime-detector/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftCreatR%2Fphp-mime-detector/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269892843,"owners_count":24491990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","detection","extension","file","file-extension","file-type","filetype","magic-numbers","mime","mime-type","mimetype","php","php5","php53","php54","php55","php56","php7","php8","phpunit"],"created_at":"2024-09-24T19:58:26.184Z","updated_at":"2025-08-11T13:09:00.775Z","avatar_url":"https://github.com/SoftCreatR.png","language":"PHP","funding_links":["https://github.com/sponsors/softcreatr"],"categories":[],"sub_categories":[],"readme":"# PHP Mime Detector\n\nDetecting the real type of (binary) files doesn't have to be difficult. Checking a file's extension is not reliable and can lead to serious security issues.\n\nThis package helps you determine the correct type of files by reading them byte by byte (up to 4096 bytes) and checking for [magic numbers](http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files).\n\nHowever, this package isn't a replacement for any security software. It simply aims to produce fewer false positives than a simple extension check would.\n\nA list of supported file types can be found on [this Wiki page](https://github.com/SoftCreatR/php-mime-detector/wiki/Supported-file-types).\n\n## Why a Separate Class?\n\nYou may wonder why we don't just rely on extensions like [Fileinfo](https://www.php.net/manual/en/book.fileinfo.php). Here's a brief background:\n\nWe develop extensions and applications for an Open Source PHP Framework, creating web software for the masses. Many of our customers and users of our free products are on shared hosting without any ability to install or manage PHP extensions. Therefore, our goal is to develop solutions with minimal dependencies while providing as much functionality as possible.\n\nWhen developing a solution that allows people to convert HEIF/HEIC files to a more \"standardized\" format (using our own external API), we had trouble detecting these files because this format isn't widely recognized by most web servers. Since checking the file extension isn't reliable, we needed to find a reusable solution that works for most of our clients. This led to the creation of our Mime Detector, based on magic number checks.\n\n## Requirements\n\n- PHP 8.1 or newer\n- [Composer](https://getcomposer.org)\n\n## Installation\n\nInstall this package using [Composer](https://getcomposer.org/) in the root directory of your project:\n\n```bash\ncomposer require softcreatr/php-mime-detector\n```\n\n## Usage\n\nHere is an example of how this package makes it easy to determine the MIME type and corresponding file extension of a given file:\n\n```php\n\u003c?php\n\nuse SoftCreatR\\MimeDetector\\MimeDetector;\nuse SoftCreatR\\MimeDetector\\MimeDetectorException;\n\nrequire 'vendor/autoload.php';\n\ntry {\n    // Create an instance of MimeDetector with the file path\n    $mimeDetector = new MimeDetector('foo.bar');\n\n    // Get the MIME type and file extension\n    $fileData = [\n        'mime_type' =\u003e $mimeDetector-\u003egetMimeType(),\n        'file_extension' =\u003e $mimeDetector-\u003egetFileExtension(),\n        'file_hash' =\u003e $mimeDetector-\u003egetFileHash(),\n    ];\n\n    // Print the result\n    echo '\u003cpre\u003e' . print_r($fileData, true) . '\u003c/pre\u003e';\n} catch (MimeDetectorException $e) {\n    die('An error occurred while trying to load the given file: ' . $e-\u003egetMessage());\n}\n```\n\n## Testing\n\nThis project uses PHPUnit for testing. To run tests, use the following command:\n\n```bash\ncomposer test\n```\n\nTo run a full test suite, you can use a set of provided test files. These files are not included in the Composer package or Git repository, so you must clone this repository and initialize its submodules:\n\n```bash\ngit clone https://github.com/SoftCreatR/php-mime-detector\ncd php-mime-detector\ngit submodule update --init --recursive\n```\n\nAfter that, install the necessary dependencies with `composer install`, and run PHPUnit as described above.\n\n## ToDo\n\n- Reduce method sizes where possible.\n- Add a method that accepts a MIME type and returns the corresponding file extension.\n- Add a method that accepts a file extension and returns a list of corresponding MIME types.\n- Add a method that returns a list of all detectable MIME types and their corresponding file extensions.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\nWhen adding new detections, please provide at least one sample file to ensure the detection works as expected.\n\n## License\n\n[ISC License](LICENSE.md)\n\nFree Software, Hell Yeah!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcreatr%2Fphp-mime-detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftcreatr%2Fphp-mime-detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftcreatr%2Fphp-mime-detector/lists"}