Download the PHP package bkstar123/laravel-uploader without Composer
On this page you can find all versions of the php package bkstar123/laravel-uploader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bkstar123/laravel-uploader
More information about bkstar123/laravel-uploader
Files in bkstar123/laravel-uploader
Package laravel-uploader
Short Description A Laravel package for handling file upload
License MIT
Informations about the package laravel-uploader
laravel-uploader
Provide a Laravel backend for handling file upload
1.Requirements
It is recommended to install this package with PHP version 7.1.3+ and Laravel Framework version 5.5+
2.Installation
composer require bkstar123/laravel-uploader
You can upload files to the backend either via a normal form submit or via AJAX calls (built by yourself or any third party plugins).
Note:
-
Form submit supports only single file upload. For multiple files upload, you should use AJAX to send files in parallel requests
-
The package also includes a frontend fileinput plugin from Krajee for quickly demonstrating the AJAX uploading. The assets included in the package are barely minimum and may not be up to date. You should consult http://plugins.krajee.com/file-input for the latest version and more advanced use cases of the plugin
-
The Krajee plugin's full version can be installed via
- In order to use the included frontend assets, run
3.Usage
3.1 In Controller
Firstly, you typehint in a controller constructor to inject a FileUpload service.
Then, in the method which is supposed to handle a file upload request, you call
Where:
- $request: full Laravel request object
- $fieldname: name of the file input field in your frontend
- $uploadSettings (optional): inline settings to customize the behavior of the backend
$uploadSettings is an asociative array with the following possible keys:
- : the root directory containing your uploaded files
- : the storage disk for file upload. Check Laravel official documentation for more details, e.g: ,
- (in bytes): the maximum size of an uploaded file that can be accepted by the backend
- : array of acceptable file extensions, e.g:
The backend default settings are as follows:
You can change these default settings by using the following environment variables in .env:
- (in bytes)
Note: The inline settings will overwrite the default ones
If the upload succeeds, will return the following data for being further persisted to database:
If the uploaded file is not valid, then will be returned and an error message will be set for
3.2 In frontend view
3.2.1 Normal form upload
(Only support single file upload)
Example:
3.2.2 AJAX uploading using default frontend assets from Krajee
(Support sending multiple files in parallel requests)
Example:
Note: When using Krajee fileinput plugin, you must return a json response from the controller method which handles the upload request as follows:
3.2.3 AJAX uploading using @bkstar18/jquery-ajax-uploader plugin (written by myself)
You can check its full documentation at https://github.com/bkstar123/jquery-ajax-uploader
Example use:
a) Installation
-
In , place the following line:
-
Then, compile your assets using :
- Alternatively, if you do not want to bundle this plugin into the main app.js, you can place the following line in :
Then, include in any view where you want to use the plugin. Remember to load JQuery before using the plugin.
b) In HTML section
c) In Javascript section
d) In Laravel Controller method
Note:
You should make sure that the page html layout has the following tag:
Otherwise, the request may be blocked by Laravel by default. See more details at https://laravel.com/docs/5.8/csrf#csrf-x-csrf-token.
The plugin automatically checks the existence of this tag, get its content and associate header with the uploading request.
Alternatively, if and only if this tag does not exist (maybe you do not want to use, or for somewhat reasons), then you can include request header before sending files to server via hook as follows:
3.2.4 AJAX uploading using @bkstar18/vue-ajax-uploader plugin (written by myself)
Check https://github.com/bkstar123/vue-ajax-uploader for details.
3.3 Physically remove an uploaded file
You can physically delete an uploaded file as in following example:
In this example, the photos table must have and columns to persist a instance.
You should physically remove an uploaded file only after deleting its relevant record in the database.