DekGenius.com
PHP
upload file laravel
public function fileUploadPost(Request $request)
{
$request->validate([
'file' => 'required|mimes:pdf,xlx,csv|max:2048',
]);
$fileName = time().'.'.$request->file->extension();
$request->file->move(public_path('uploads'), $fileName);
return back()
->with('success','You have successfully upload file.')
->with('file',$fileName);
}
upload file in laravel
//first run this command to generate symbolic link
php artisan storage:link
//then in controller function
$fileTemp = $request->file('file');
if($fileTemp->isValid()){
$fileExtension = $fileTemp->getClientOriginalExtension();
$fileName = Str::random(4). '.'. $fileExtension;
$path = $fileTemp->storeAs(
'public/documents', $fileName
);
}
//above code will save your file in 'storage/app/public/documents' location
//and using symbolic link we can access this file here 'public/storage/documents'
//Now Open or download file in blade template
<a href="{{url(Storage::url($document['file']))}}">Open/Download</a>
laravel file upload
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class FileUploadController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function fileUpload()
{
return view('fileUpload');
}
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function fileUploadPost(Request $request)
{
$request->validate([
'file' => 'required|mimes:pdf,xlx,csv|max:2048',
]);
$fileName = time().'.'.$request->file->extension();
$request->file->move(public_path('uploads'), $fileName);
return back()
->with('success','You have successfully upload file.')
->with('file',$fileName);
}
}
laravel file uploads
$path = Storage::putFile('avatars', $request->file('avatar'));
file upload in laravel
$design_file_name=null;
if($request->image != null){
$design_file_name ="clinic_" . md5(time()) . "_" . $request->image->getClientOriginalName();
$design_file_path = "clinic_" . md5(time()) . "_" . $request->image->getClientOriginalName();
$path = public_path().'/storage/clinic';
$uplaod = $request->image->move($path,$design_file_name);
}
#HTML
<div class="col-lg-6 col-md-6">
<div class="form-group">
<div class="choose-file">
<label for="image">
<div>{{__('Choose file here')}}</div>
<input class="form-control" name="image" type="file" id="image" accept="image/*" data-filename="profile_update">
</label>
<p class="profile_update"></p>
</div>
@error('avatar')
<span class="invalid-feedback text-danger text-xs" role="alert">{{ $message }}</span>
@enderror
</div>
<span class="clearfix"></span>
<span class="text-xs text-muted">{{ __('Please upload a valid image file. Size of image should not be more than 2MB.')}}</span>
</div>
<input type="submit" value="{{__('Save')}}" class="btn-create badge-blue">
</div>
upload laravel
//upload.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<title>Laravel File Upload</title>
<style>
.container {
max-width: 500px;
}
dl, ol, ul {
margin: 0;
padding: 0;
list-style: none;
}
</style>
</head>
<body>
<div class="container mt-5">
<form action="{{route('fileUpload')}}" method="post" enctype="multipart/form-data">
<h3 class="text-center mb-5">Upload File in Laravel</h3>
@csrf
@if ($message = Session::get('success'))
<div class="alert alert-success">
<strong>{{ $message }}</strong>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="custom-file">
<input type="file" name="file" class="custom-file-input" id="chooseFile">
<label class="custom-file-label" for="chooseFile">Select file</label>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-block mt-4">
Upload Files
</button>
</form>
</div>
</body>
</html>
//web.php
use IlluminateSupportFacadesRoute;
use AppHttpControllersUploadController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/upload-file', [UploadController::class, 'createForm']);
Route::post('/upload-file', [UploadController::class, 'fileUpload'])->name('fileUpload');
//UploadController.php
<?php
namespace AppHttpControllers;
use AppModelsUpload;
use IlluminateSupportFacadesFile;
use IlluminateHttpRequest;
class UploadController extends Controller
{
//
public function createForm(){
return view('upload');
}
public function fileUpload(Request $req){
// $req->validate([
// 'file' => 'required|mimes:csv,txt,xlx,xls,pdf|max:2048'
// ]);
$fileModel = new Upload;
if($req->file()) {
$fileName = time().'_'.$req->file->getClientOriginalName();
$filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = time().'_'.$req->file->getClientOriginalName();
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->save();
return back()
->with('success','File has been uploaded.')
->with('file', $fileName);
}
}
}
//upload.php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Upload extends Model
{
// use HasFactory;
protected $table = 'tbl_upload';
}
laravel file upload
$('form').submit(function(event) {
event.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url: '{{ url('/agents') }}',
type: 'POST',
data: formData,
success: function(result)
{
location.reload();
},
error: function(data)
{
console.log(data);
}
});
});
© 2022 Copyright:
DekGenius.com