PHP code example of awaisjameel / base64filehandler
1. Go to this page and download the library: Download awaisjameel/base64filehandler 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/ */
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
$path = Base64FileHandler::store($base64Data);
use AwaisJameel\Base64FileHandler\Base64FileHandler;
class AvatarController
{
public function __construct(private Base64FileHandler $handler)
{
}
public function store(Request $request)
{
$path = $this->handler->store($request->input('avatar'));
// ...
}
}
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
// Uses the configured default disk, path, and allowed extensions.
$path = Base64FileHandler::store($base64Data);
// Every argument after the base64 payload is optional and overrides config for this call only.
$path = Base64FileHandler::store(
$base64Data,
disk: 's3',
path: 'avatars/',
originalName: 'profile-picture.jpg',
allowedExtensions: ['jpg', 'jpeg', 'png'],
);
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
use AwaisJameel\Base64FileHandler\Exceptions\InvalidImageException;
use AwaisJameel\Base64FileHandler\Exceptions\MimeTypeMismatchException;
try {
Base64FileHandler::isValidImage($base64Data);
// The data decodes to one of the configured valid image extensions.
} catch (InvalidImageException | MimeTypeMismatchException $e) {
// Not a valid image.
}
// Or check against a custom set of extensions for this call only:
Base64FileHandler::isValidImage($base64Data, ['png', 'webp']);
use AwaisJameel\Base64FileHandler\Exceptions\Base64FileHandlerException;
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
try {
$path = Base64FileHandler::store($base64Data);
} catch (Base64FileHandlerException $e) {
report($e);
abort(422, $e->getMessage());
}
use AwaisJameel\Base64FileHandler\Exceptions\Base64FileHandlerException;
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
use AwaisJameel\MimeTypes\Exceptions\MimeTypesException;
try {
$path = Base64FileHandler::store($base64Data);
} catch (Base64FileHandlerException | MimeTypesException $e) {
report($e);
abort(422, 'Unable to process the uploaded file.');
}