File Upload Process in Laravel Framework
21-01-2019We can easly integrate file upload process in Laravel as follows:
1. Add following codes to create.blade.php file:
<div class="box box-primary"> <div class="box-body"> <div class="row"> {!! Form::open(['route' => 'applications.store','files'=>true]) !!} <div class="form-group col-sm-6"> {!! Form::label('image', 'Image:') !!} @if(isset($application->image)) <img src="{!! $application->image !!}" width="125" height="125"/> <input type="hidden" name="image-old" value="{!! $application->image !!}"> @endif <div class="form-control"> {!! Form::file('image') !!} </div> {!! Form::close() !!} </div> </div> </div> </div>
2. Add following codes to edit.blade.php file:
<div class="box box-primary"> <div class="box-body"> <div class="row"> {!! Form::model($application, ['route' => ['applications.update', $application->id], 'method' => 'patch','files'=>true]) !!} <div class="form-group col-sm-6"> {!! Form::label('image', 'Image:') !!} @if(isset($application->image)) <img src="{{asset("$application-/>image")}}" width="125" height="125"/> <input type="hidden" name="image-old" value="{{$application->image}}"> @endif <div class="form-control"> {!! Form::file('image') !!} </div> </div> {!! Form::close() !!} </div> </div> </div>
3. In Controller class:
public function store(CreateApplicationRequest $request) { $fileUtil = new FileUtil(); $filePath = $fileUtil->saveSingleFile($request, "image"); $input = $request->all(); $input["image"] = $filePath; $application = $this->applicationRepository->create($input); alert()->success('Application saved successfully.'); return redirect(route('applications.index')); } public function update($id, UpdateApplicationRequest $request) { $application = $this->applicationRepository->findWithoutFail($id); if (empty($application)) { Flash::error('Application not found'); return redirect(route('applications.index')); } $fileUtil = new FileUtil(); $filePath = $fileUtil->saveSingleFile($request, "image"); if ($filePath == null) $filePath = $request->input("image-old"); $input = $request->all(); $input["image"] = $filePath; $application = $this->applicationRepository->update($input, $id); alert()->success('Application updated successfully.'); return redirect()->route('applications.edit', $application->id); }
FileUtil class:
use Illuminate\Http\Request; class FileUtil { /** * @param Request $request * @param string $inputName * @return string saved file path */ public static function saveSingleFile(Request $request, $inputName = "file") { $file = $request->file($inputName); if ($file != null) { $fileName = Storage::disk("uploads")->put("", $file); if ($request->secure()){ $filePath = "https://" . $request->getHost() . "/uploads/" . $fileName; } else{ $filePath = "http://" . $request->getHost() . "/uploads/" . $fileName; } return $filePath; } else { return null; } } }
config/filesystem.php file for Storage:
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'visibility' => 'public', ], 'uploads' => [ 'driver' => 'local', 'root' => public_path().'/uploads' , 'visibility' => 'public', ], 's3' => [ 'driver' => 's3', 'key' => 'your-key', 'secret' => 'your-secret', 'region' => 'your-region', 'bucket' => 'your-bucket', ], ],