2023 Working Solution:
Just use that amazing Laravel Package and do your job in just three lines:
Reference: https://github.com/oldravian/multi-source-file-uploader
$file_uploader_factory = new
\OldRavian\FileUploader\Factories\FileUploaderFactory();
$file_uploader = $file_uploader_factory->build("base64");
//first parameter should be a string (base64 encoded string)
//second parameter is optional, if you leave that parameter then default settings will be used
$data = $file_uploader->upload($encoded_image, $uploadSettings);
The above function will return an associative array like:
[
'filename' => 'uploaded file name',
'path' => 'path to file location relative to the disk storage',
'url' => 'public url to access the file in browser'
]
$uploadSettings is an associative array with the following possible keys:
- directory: the root directory containing your uploaded files
- disk: the storage disk for file upload. Check Laravel official documentation for
more details, e.g: public, s3
- maxFileSize (in bytes): the maximum size of an uploaded file
- allowedExtensions: array of acceptable file extensions, e.g: ['jpg', 'png', 'pdf']
In addition to base64, this package also supports file uploading by file object and URL.