Download the PHP package awaisjameel/base64filehandler without Composer
On this page you can find all versions of the php package awaisjameel/base64filehandler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download awaisjameel/base64filehandler
More information about awaisjameel/base64filehandler
Files in awaisjameel/base64filehandler
Package base64filehandler
Short Description A Laravel package for handling and storing base64 encoded files.
License MIT
Homepage https://github.com/awaisjameel/base64filehandler
Informations about the package base64filehandler
Base64 File Handler
Base64FileHandler decodes data: URI / raw base64 payloads — the kind you get from a JS canvas export, a signature pad, or a mobile app's JSON upload — validates them, and stores them on any Laravel filesystem disk. It resolves MIME types and extensions using awaisjameel/mimetypes, so file-type detection stays accurate without a hardcoded, ever-stale lookup table baked into this package.
Features
- Store base64 data on any disk — local, S3, or any other Laravel filesystem, with a configurable default disk/path and per-call overrides.
- Extension allow-listing — restrict which file types may be stored, globally via config or per call.
- Image validation — check that a base64 payload decodes to one of a configurable set of image extensions.
- File info inspection — pull the MIME type, extension, size, and raw decoded bytes out of a base64 string without storing it.
- Collision-safe filenames — generated paths are unique per call, even for concurrent requests uploading the same original filename.
- Typed exceptions — a distinct exception class per failure mode, so you can catch precisely what you expect instead of a generic
Exception. - Container-aware configuration — resolve the handler via the facade or dependency injection and it honours your published config, environment variables, and any runtime
config()overrides.
Requirements
| Base64FileHandler | PHP | Laravel |
|---|---|---|
| ^3.0 | ^8.2 | ^12.0, ^13.0 |
The fileinfo PHP extension is required. Laravel 12 supports PHP 8.2–8.5;
Laravel 13 requires PHP 8.3–8.5. Development installs use the latest compatible
Pest version (3 on PHP 8.2, 4 on PHP 8.3 or Laravel 12, and 5 on PHP 8.4–8.5
with Laravel 13).
Upgrading from 2.x
Version 3 drops Laravel 10 and 11. MIME types are now detected from the decoded
bytes for every upload. A data URI declaring a different type throws
MimeTypeMismatchException before storage or image validation. Correct mislabeled
uploads at their source; raw base64 remains supported. Data URI metadata is matched
case-insensitively, and parameters such as charset=UTF-8 are accepted.
isValidImage() requires an image MIME type and recognizable image dimensions in
addition to the configured extension list. It inspects image metadata; it does not
fully decode pixels or sanitize image content. Storage failures return the package's
UnableToStoreFileException, preserving any underlying filesystem exception.
Installation
Install the package via Composer:
The service provider and Base64FileHandler facade are auto-discovered — there's nothing else to register.
Optionally publish the config file:
This is the config that gets published:
Usage
The recommended way to use the package is through its facade — resolving it this way always honours your published config and any runtime config() overrides:
Prefer dependency injection? The underlying AwaisJameel\Base64FileHandler\Base64FileHandler class is bound as a singleton in the container, so you can type-hint it anywhere:
Storing a base64 file
$base64Data may be a full data URI (data:image/png;base64,...) or a raw base64 string with no prefix. The MIME type is detected from the decoded bytes, and a supplied MIME label must match it. The returned $path is the file's path on the given disk (suitable for Storage::disk($disk)->url($path), ->get($path), etc.).
If originalName is given, its slugified filename is used as a prefix so stored paths stay readable; a unique suffix is always appended so concurrent uploads — even of the same original filename — never collide or overwrite one another.
Validating that base64 data is an image
Inspecting base64 data without storing it
Exceptions
Every method throws a specific exception for each failure mode instead of a generic Exception, so you can catch exactly what you expect:
| Exception | Thrown when |
|---|---|
AwaisJameel\Base64FileHandler\Exceptions\InvalidBase64DataException |
The given string isn't valid base64 data. |
AwaisJameel\Base64FileHandler\Exceptions\UnableToDetermineMimeTypeException |
The decoded content's MIME type couldn't be detected. |
AwaisJameel\Base64FileHandler\Exceptions\MimeTypeMismatchException |
The data URI's MIME label conflicts with the decoded content. |
AwaisJameel\Base64FileHandler\Exceptions\UnsupportedFileExtensionException |
The resolved file extension isn't in the allowed extensions list (store() only). |
AwaisJameel\Base64FileHandler\Exceptions\InvalidImageException |
The extension is disallowed or the decoded content lacks an image MIME type and recognizable dimensions (isValidImage() only). |
AwaisJameel\Base64FileHandler\Exceptions\UnableToStoreFileException |
The filesystem disk rejected the write (store() only). |
All of the above extend the package's base AwaisJameel\Base64FileHandler\Exceptions\Base64FileHandlerException, so you can catch everything this package itself throws in one place:
Resolving a MIME type or extension is delegated to awaisjameel/mimetypes, which can itself throw AwaisJameel\MimeTypes\Exceptions\UnknownMimeTypeOrExtensionException (an unrecognized MIME type/extension) or AwaisJameel\MimeTypes\Exceptions\UnableToFetchMimeTypesException (its MIME type source is unreachable and its offline fallback has been disabled). Both extend AwaisJameel\MimeTypes\Exceptions\MimeTypesException — catch that too if you want a single catch block covering everything either package can throw:
Configuration reference
| Key | Env variable | Default | Description |
|---|---|---|---|
disk |
BASE64_FILE_HANDLER_DISK |
public |
The default filesystem disk files are stored on. |
path |
BASE64_FILE_HANDLER_PATH |
uploads/ |
The default path within the disk files are stored under. |
allowed_extensions |
— | [] (all allowed) |
Extensions store() will accept. Matched case-insensitively. |
valid_image_extensions |
— | ['jpeg', 'jpg', 'png', 'gif', 'webp'] |
Extensions isValidImage() treats as images. Matched case-insensitively. |
Every option above can also be overridden per call via store()'s and isValidImage()'s optional arguments, or by constructing the class directly with a custom config array:
Note that a directly-instantiated new Base64FileHandler(...) does not read your published config file — it only uses the defaults above merged with whatever you pass in. Resolve it via the facade or the container (as shown under Usage) if you want your published config file honoured.
Testing
The package's MimeTypes dependency resolves its data over HTTP; the test suite fakes that request (see tests/TestCase.php) so tests run fully offline and deterministically, using the bundled MIME type snapshot as a fallback. Keep that in mind if you write your own tests against this package — fake the same way, or Http::fake() a MIME type response yourself.
Or with coverage:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of base64filehandler with dependencies
ext-fileinfo Version *
awaisjameel/mimetypes Version ^2.0||^3.0
illuminate/filesystem Version ^12.0||^13.0
illuminate/support Version ^12.0||^13.0
league/flysystem Version ^3.36.0
spatie/laravel-package-tools Version ^1.93.3