|
| 1 | +# Multi Source File Uploader |
| 2 | +> A Laravel package for handling file upload from multiple sources like file object, url or base64 encoded data |
| 3 | +
|
| 4 | +## Current Features |
| 5 | + |
| 6 | +- file uploading from multiple sources like file object, url or base64 encoded data |
| 7 | +- file validation (based on extension and size) |
| 8 | +- support for handling multiple files |
| 9 | + |
| 10 | +## Upcoming Features |
| 11 | + |
| 12 | +- file compression |
| 13 | +- support for file deletion |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +It is recommended to install this package with **PHP version 7.2+** and **Laravel Framework version 5.5+** |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +``` |
| 22 | +composer require oldravian/multi-source-file-uploader |
| 23 | +``` |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +Copy configuration to your project: |
| 28 | + |
| 29 | +``` |
| 30 | +php artisan vendor:publish --provider="OldRavian\FileUploader\FileUploaderServiceProvider" |
| 31 | +``` |
| 32 | + |
| 33 | +By executing above command the package configuration will be published to **config/old-ravian-file-uploader.php** |
| 34 | + |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +Firstly, you need to initialize the specific FileUploader instance using ```FileUploaderFactory``` |
| 39 | +```php |
| 40 | +$file_uploader_factory = new \OldRavian\FileUploader\Factories\FileUploaderFactory(); |
| 41 | +$file_uploader = $file_uploader_factory->build(here you need to provide the file source name, it could be "object", "url" or "base64"); |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## Upload File From Object |
| 46 | +In your controller method: |
| 47 | + |
| 48 | +```php |
| 49 | + |
| 50 | +public function uploadFile(Request $request) |
| 51 | +{ |
| 52 | + $uploadSettings = ["directory"=>"mention directory", "disk"=>"mention disk", "maxFileSize"=>"mention size in bytes", "allowedExtensions"=>[mention extensions]]; |
| 53 | + $file_uploader_factory = new \OldRavian\FileUploader\Factories\FileUploaderFactory(); |
| 54 | + $file_uploader = $file_uploader_factory->build("object"); |
| 55 | + |
| 56 | + //first parameter should be an instance of \Illuminate\Http\UploadedFile |
| 57 | + //second parameter is optional, if you leave that parameter then default settings will be used |
| 58 | + $data = $file_uploader->upload($request->file, $uploadSettings); //it will return an array |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +**$uploadSettings** is an asociative array with the following possible keys: |
| 63 | +- ```directory```: the root directory containing your uploaded files |
| 64 | +- ```disk```: the storage disk for file upload. Check Laravel official documentation for more details, e.g: ```public```, ```s3``` |
| 65 | +- ```maxFileSize``` (in bytes): the maximum size of an uploaded file |
| 66 | +- ```allowedExtensions```: array of acceptable file extensions, e.g: ```['jpg', 'png', 'pdf']``` |
| 67 | + |
| 68 | +The backend default settings are as follows: |
| 69 | +``` |
| 70 | +- 'directory': 'media' |
| 71 | +- 'disk': 'public' |
| 72 | +- 'maxFileSize': 50 MB |
| 73 | +- 'allowedExtensions': 'png','jpg','jpeg','mp4','doc','docx','ppt','pptx','xls','xlsx','txt','pdf' |
| 74 | +``` |
| 75 | + |
| 76 | +You can change these default settings by using the following environment variables in .env: |
| 77 | +- ```OLDRAVIAN_File_UPLOADER_DEFAULT_DISK``` |
| 78 | +- ```OLDRAVIAN_File_UPLOADER_DEFAULT_DIRECTORY``` |
| 79 | +- ```OLDRAVIAN_File_UPLOADER_DEFAULT_MAX_FILE_SIZE``` (in bytes) |
| 80 | + |
| 81 | +If the upload succeeds,```$file_uploader->upload($request->file, $uploadSettings)``` will return the following data for being further persisted to database: |
| 82 | + |
| 83 | +```php |
| 84 | +[ |
| 85 | + 'filename' => 'uploaded file name', |
| 86 | + 'path' => 'path to file location relative to the disk storage', |
| 87 | + 'url' => 'public url to access the file in browser' |
| 88 | +] |
| 89 | +``` |
| 90 | + |
| 91 | +If the uploaded file is not valid, then ```false``` will be returned and an error message will be set for ```$file_uploader->uploadError``` |
| 92 | + |
| 93 | +## Upload File From URL |
| 94 | + |
| 95 | +```php |
| 96 | + |
| 97 | +$file_uploader_factory = new \OldRavian\FileUploader\Factories\FileUploaderFactory(); |
| 98 | +$file_uploader = $file_uploader_factory->build("url"); |
| 99 | + |
| 100 | +//first parameter should be a string url |
| 101 | +//second parameter is optional, if you leave that parameter then default settings will be used |
| 102 | +$data = $file_uploader->upload($request->url, $uploadSettings); |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Upload File From Base64 Encoded data |
| 107 | + |
| 108 | +```php |
| 109 | + |
| 110 | +$file_uploader_factory = new \OldRavian\FileUploader\Factories\FileUploaderFactory(); |
| 111 | +$file_uploader = $file_uploader_factory->build("base64"); |
| 112 | + |
| 113 | +//first parameter should be a string (base64 encoded string) |
| 114 | +//second parameter is optional, if you leave that parameter then default settings will be used |
| 115 | +$data = $file_uploader->upload($request->base64_str, $uploadSettings); |
| 116 | +``` |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +## Handling Multiple File Uploads |
| 121 | + |
| 122 | +```php |
| 123 | + |
| 124 | +$file_uploader_factory = new \OldRavian\FileUploader\Factories\FileUploaderFactory(); |
| 125 | +$file_uploader = $file_uploader_factory->build("object or url or base64"); |
| 126 | + |
| 127 | +$urls_array = ["first file url", "second file url", "third file url"]; |
| 128 | +//first parameter should be an array (it could be an array of objects or urls or base64 encoded strings array based on what you mentioned in the above line) |
| 129 | +//second parameter is optional, if you leave that parameter then default settings will be used |
| 130 | +$data_array = $file_uploader->uploadMany($urls_array, $uploadSettings); //$data_array is a 2d array |
| 131 | +``` |
| 132 | + |
0 commit comments