file-uploader-laravel maintained by sdtech
file-uploader-laravel | A Library to upload image and file with validation.
About
A lightweight, framework-friendly Laravel package to validate and upload images/files with zero headaches. Now supports three upload targets out of the box: local storage, public folder, and Amazon S3. Image operations (resize, format conversion, quality) are handled via Intervention/Image.
Current features:
- Upload to local storage (
storage/app/public) - Upload to public folder (
public/) - Upload to Amazon S3 (via Flysystem S3 adapter)
- Image validation, resizing, and format conversion (webp/png/jpeg/gif/bmp)
- Simple, consistent response structure
Requirements
Installation
- From your projects root folder in terminal run:
composer require sdtech/file-uploader-laravel
- In config/app.php, add it:
'providers' => [
// Other Service Providers...
Sdtech\FileUploaderLaravel\Providers\FileUploadLaravelServiceProviders::class,
],
- Publish the packages views, config file, assets, and language files by running the following from your projects root folder:
php artisan vendor:publish --tag=fileuploaderlaravel
configuration
- Go to your config folder, then open "fileuploaderlaravel.php" file
- here you must add that info or add the info to your .env file .
'ALLOWED_IMAGE_TYPE' => env('ALLOWED_IMAGE_TYPE'),
'MAX_UPLOAD_IMAGE_SIZE' => env('MAX_UPLOAD_IMAGE_SIZE') // default 2048 KB
'DEFAULT_IMAGE_FORMAT' => env('DEFAULT_IMAGE_FORMAT') // default 'webp',
'DEFAULT_IMAGE_QUALITY' => env('DEFAULT_IMAGE_QUALITY') // default 80,
'AWS_ACCESS_KEY_ID' => env('AWS_ACCESS_KEY_ID'),
'AWS_SECRET_ACCESS_KEY' => env('AWS_SECRET_ACCESS_KEY'),
'AWS_DEFAULT_REGION' => env('AWS_DEFAULT_REGION'),
'AWS_BUCKET' => env('AWS_BUCKET'),
'AWS_URL' => env('AWS_URL')
For S3 uploads, ensure your application's config/filesystems.php has the s3 disk configured.
The following dependencies are installed automatically when you install this package:
- league/flysystem-aws-s3-v3
- aws/aws-sdk-php
If anyone faces any issues to install it, you can use:
composer require sdtech/file-uploader-laravel --ignore-platform-reqs
Example env:
FILESYSTEM_DISK=public
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...
AWS_DEFAULT_REGION=ap-south-1
AWS_BUCKET=your-bucket
AWS_URL=https://your-cdn-or-bucket-url
- run this command
php artisan storage:link
sudo chmod -R 777 storage
Uses
- We provide a sample code of functionality that will help you to integrate easily
use Sdtech\FileUploaderLaravel\Service\FileUploadLaravelService;
class UploadController extends Controller
{
public function uploadImg(Request $request) {
$service = new FileUploadLaravelService();
$reqFile = $request->img;
$path = 'uploads';
$response = $service->uploadImageInStorage($reqFile,$path);
return $response;
}
public function uploadImgToS3(Request $request) {
$service = new FileUploadLaravelService();
$reqFile = $request->img;
$path = 'uploads';
// optional: width/height/quality/format
return $service->uploadImageInS3($reqFile, $path);
}
public function uploadFileToS3(Request $request) {
$service = new FileUploadLaravelService();
$reqFile = $request->file('file');
$path = 'files';
return $service->uploadFileInS3($reqFile, $path);
}
}
in the same way you can use other function as well
some functions
upload image in storage folder
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) file path where upload iamge
@param STRING $oldFile (optional) old file name // $oldFile = '1720705563668fe21b791d2.png';
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
@param STRING $format (optional) image output format default = webp
@param INT $width (optional) image width
@param INT $height (optional) image height
@param INT $quality (optional) image quality default = 80
uploadImageInStorage($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="", $format='',$width="",$height=null,$quality=null)
upload image in main public folder
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) file path where upload iamge
@param STRING $oldFile (optional) old file name
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
@param STRING $format (optional) image output format default = webp
@param INT $width (optional) image width
@param INT $height (optional) image height
@param INT $quality (optional) image quality default = 80
uploadImageInPublic($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="",$format='',$width="",$height=null,$quality=null)
upload image to s3
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) s3 key prefix (folder) e.g. images/products
@param STRING $oldFile (optional) old file name
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
@param STRING $format (optional) image output format default = webp
@param INT $width (optional) image width
@param INT $height (optional) image height
@param INT $quality (optional) image quality default = 80
uploadImageInS3($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="",$format='',$width="",$height=null,$quality=null)
upload file in storage folder
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) file path where upload iamge
@param STRING $oldFile (optional) old file name
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
uploadFileInStorage($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="")
upload file in public folder
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) file path where upload iamge
@param STRING $oldFile (optional) old file name
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
ploadFileInPublic($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="")
upload file to s3
@param FILE $reqFile (mandetory) uploaded file
@param STRING $path (mandetory) s3 key prefix (folder)
@param STRING $oldFile (optional) old file name
@param ARRAY $allowedImageType (optional) allowed image type like ["png","webp","jpeg"]
@param INT $maxSize (optional) max upload size in KB 1024KB = 1MB
uploadFileInS3($reqFile,$path,$old_file="",$allowedImageType=[],$maxSize="")
delete file path
unlinkFile($path,$oldFile)
get file view path for storage folder
showStorageFileViewPath($path,$fileName)
get file view path for public folder
showFileViewPath($path,$fileName)
get allowed image type
allowedTypes()
get allowed file type
allowedFileExtensions()