public/
directory.public/
directory.Route::get('/','UsersController@index');
Route::post('/users/fileupload/','UsersController@fileupload')->name('users.fileupload');
public/
directory.public/
directory.php artisan make:controller UsersController
public/
directory.Assign upload location to $destinationPath
variable. Create a directory if not exists.
Get the file extension and initialize valid file extension in $validextensions
Array.
Check the file extension is exists in $validextension
Array. If exists then rename the file and upload it.
Completed Code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class UsersController extends Controller {
public function index(){
return view('users.index');
}
/*
File upload
*/
public function fileupload(Request $request){
if($request->hasFile('file')) {
// Upload path
$destinationPath = 'files/';
// Create directory if not exists
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
// Get file extension
$extension = $request->file('file')->getClientOriginalExtension();
// Valid extensions
$validextensions = array("jpeg","jpg","png","pdf");
// Check extension
if(in_array(strtolower($extension), $validextensions)){
// Rename file
$fileName = str_slug(Carbon::now()->toDayDateTimeString()).rand(11111, 99999) .'.' . $extension;
// Uploading file to given path
$request->file('file')->move($destinationPath, $fileName);
}
}
}
}
Create file –
public/
directory.Include CSS and JS –
public/
directory.Dropzone container –
public/
directory.Script –
public/
directory.Completed Code
<!DOCTYPE html>
<html>
<head>
<title>Drag and Drop file upload with Dropzone in Laravel</title>
<!-- Meta -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="{{asset('dropzone/dist/min/dropzone.min.css')}}">
<!-- JS -->
<script src="{{asset('dropzone/dist/min/dropzone.min.js')}}" type="text/javascript"></script>
</head>
<body>
<div class='content'>
<!-- Dropzone -->
<form action="{{route('users.fileupload')}}" class='dropzone' >
</form>
</div>
<!-- Script -->
<script>
var CSRF_TOKEN = document.querySelector('meta[name="csrf-token"]').getAttribute("content");
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(".dropzone",{
maxFilesize: 3, // 3 mb
acceptedFiles: ".jpeg,.jpg,.png,.pdf",
});
myDropzone.on("sending", function(file, xhr, formData) {
formData.append("_token", CSRF_TOKEN);
});
</script>
</body>
</html>
You need to also pass CSRF token to upload the file using Dropzone. For this explicitly initialize dropzone and pass the token.
Recommended Reading
☞ Create a Laravel Vue Single Page App
☞ Laravel Repository Pattern Implementation
☞ Laravel 6 Release New Features and Upgrade
☞ Instructions to Create your first Laravel package
☞ Build a simple blog + multiple image upload with Laravel & Vue
☞ How to set lifetime expiration time of passport access token in Laravel
☞ Upgrading Laravel To 6.0 From 5.8
☞ Laravel Custom Casts Package
☞ Putting a Laravel App into Production
#laravel