brandembassy/file-type-detector
Detect file type and MIME by filename extension or by inspecting binary/stream content. Returns category, format, and MIME on success (or false). Includes a helper to resolve MIME only. Install via composer: brandembassy/file-type-detector.
Storage facade), validation (custom rules), and HTTP (request file handling).FormRequest or Validator to auto-detect MIME types.| Risk Area | Mitigation Strategy |
|---|---|
| False Positives | Combine with Laravel’s mime-type package for cross-verification. |
| Performance | Cache detection results (e.g., Redis) for frequent files. |
| Stream Handling | Test with large files (>100MB) to validate memory/stream buffer limits. |
| PHP 8.2+ Dependency | Ensure CI/CD pipelines test against Laravel’s supported PHP versions (8.1+). |
| Maintenance | Monitor forks of the upstream repo for critical updates (low risk due to MIT license). |
application/octet-stream)?.doc)?Storage::disk()->put() for post-upload processing.DetectMimeType rule for form requests.FileDetected events to trigger downstream actions (e.g., virus scanning).FileReader) to reduce server load.Detector::detectByContent().use BrandEmbassy\FileTypeDetector\Detector;
$fileInfo = Detector::detectFromContent($request->file('document')->getContent());
if (!$fileInfo) throw new \Exception("Unsupported file type");
use Illuminate\Validation\Rule;
Rule::make(function ($attribute, $value, $fail) {
$mime = Detector::getMimeType($value);
if (!in_array($mime, ['image/jpeg', 'image/png'])) {
$fail('Unsupported format.');
}
});
FileDetected events to decouple detection from business logic:
event(new FileDetected($fileInfo, $userId));
Cache::remember()) for repeated detections.symfony/mime (can use both for redundancy).finfo_file() (PHP’s native function) if the package fails.| Step | Priority | Dependencies |
|---|---|---|
| 1. Core Integration | High | Laravel Filesystem, Validation |
| 2. Validation Rules | Medium | Step 1 |
| 3. Event System | Low | Step 1, Laravel Events |
| 4. Caching | Low | Step 1, Redis/Database |
| 5. Async Processing | Optional | Step 1, Laravel Queues |
.webp).finfo_file() if major bugs emerge (low risk due to MIT license).spatie/laravel-mime-types evolves).Detector::detectByContent($file, true) for verbose output..pdf, .zip) and their expected MIMEs.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Detection Fails | Rejected valid files | Fallback to finfo_file() |
| Stream Corruption | Partial reads | Retry with fseek() reset |
| High Load | Slow responses | Rate limiting + async queue |
| New File Types | Unsupported formats | Contribute to repo or extend locally |
detectByContent() vs. detectByFilename().How can I help you explore Laravel packages today?