PHP code example of softcreatr / php-mime-detector
1. Go to this page and download the library: Download softcreatr/php-mime-detector library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
softcreatr / php-mime-detector example snippets
use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeDetectorException;
); // image/png
echo $detector->getFileExtension(); // png
echo $detector->getFileHash(); // crc32 hash of the file contents
} catch (MimeDetectorException $exception) {
// React to unreadable files or unsupported formats.
echo $exception->getMessage();
}
$detector = new MimeDetector(__DIR__ . '/example.png');
// Retrieve the canonical extension for a MIME type.
$extension = $detector->getExtensionForMimeType('image/jpeg'); // "jpg"
// List every MIME type that corresponds to the given extension.
$mimeTypes = $detector->getMimeTypesForExtension('heic');
// Fetch the complete map as [mimeType => list of extensions].
$catalogue = $detector->listAllMimeTypes();
declare(strict_types=1);
use App\MimeDetector\CustomContainerDetector;
use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeDetectorException;
use SoftCreatR\MimeDetector\MimeTypeDetector;
use SoftCreatR\MimeDetector\MimeTypeRepository;
meTypeDetector::extend(
'custom-container',
static function (): array {
return [
new CustomContainerDetector(),
// More detectors can be returned from the same extension when needed.
];
},
priority: 150,
);
// 3) Resolve files like usual – the default pipeline now
namespace App\MimeDetector;
use SoftCreatR\MimeDetector\Attribute\DetectorCategory;
use SoftCreatR\MimeDetector\Contract\FileSignatureDetectorInterface;
use SoftCreatR\MimeDetector\Detection\DetectionContext;
use SoftCreatR\MimeDetector\Detection\MimeTypeMatch;
#[DetectorCategory('custom')]
final class CustomContainerDetector implements FileSignatureDetectorInterface
{
public function detect(DetectionContext $context): ?MimeTypeMatch
{
$buffer = $context->buffer();
if ($buffer->checkForBytes([0x43, 0x55, 0x53, 0x54])) { // "CUST"
return new MimeTypeMatch('custom', 'application/x-custom');
}
return null;
}
}
use SoftCreatR\MimeDetector\MimeTypeRepository;
$repository = MimeTypeRepository::createDefault();
$repository->register('custom', 'application/x-custom');
use SoftCreatR\MimeDetector\MimeDetector;
use SoftCreatR\MimeDetector\MimeTypeDetector;
MimeTypeDetector::extend(
'custom-container',
new CustomContainerDetector(),
priority: 50, // run before the bundled detectors
);
$detector = new MimeDetector(__DIR__ . '/file.cust', $repository);
$match = $detector->getMimeType(); // application/x-custom-container
use SoftCreatR\MimeDetector\Detection\DetectorPipeline;
use SoftCreatR\MimeDetector\Detector\ArchiveSignatureDetector;
use SoftCreatR\MimeDetector\Detector\DocumentSignatureDetector;
use SoftCreatR\MimeDetector\Detector\ExecutableSignatureDetector;
use SoftCreatR\MimeDetector\Detector\FontSignatureDetector;
use SoftCreatR\MimeDetector\Detector\ImageSignatureDetector;
use SoftCreatR\MimeDetector\Detector\MediaSignatureDetector;
use SoftCreatR\MimeDetector\Detector\MiscSignatureDetector;
use SoftCreatR\MimeDetector\Detector\XmlSignatureDetector;
use SoftCreatR\MimeDetector\Detector\ZipSignatureDetector;
use SoftCreatR\MimeDetector\MimeDetector;
$pipeline = DetectorPipeline::create(
new CustomContainerDetector(),
new ImageSignatureDetector(),
new ZipSignatureDetector(),
new ArchiveSignatureDetector(),
new MediaSignatureDetector(),
new DocumentSignatureDetector(),
new FontSignatureDetector(),
new ExecutableSignatureDetector(),
new MiscSignatureDetector(),
new XmlSignatureDetector(),
);
$detector = new MimeDetector(__DIR__ . '/file.cust', $repository, $pipeline);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.