How to save the name of a downloaded file in a table using Laravel 5.1

I need help saving the name of the downloaded file in a database table using laravel 5.1.

My controller code for saving snapshot information

public function store(Request $request) { if($request->hasFile('img_filename')) { $destinationPath="offerimages"; $file = $request->file('img_filename'); $filename=$file->getClientOriginalName(); $request->file('img_filename')->move($destinationPath,$filename); } $input=$request->all(); Offer_image::create($input); return redirect('offerimage'); } 

My view code for image reception

 {!! Form::open(array('route'=>'offerimage.store','role'=>'form','files'=>true)) !!} <div class="box-body"> <div class="form-group"> {!! Form::label('img_name','Name') !!} {!! Form::text('img_name', $value = null, $attributes = array('class'=>'form-control','id'=>'img_name','required')) !!} </div> <div class="form-group"> {!! Form::label('img_description','Description') !!} {!! Form::textarea('img_description', $value = null, $attributes = array('class'=>'form-control','id'=>'img_description','required')) !!} </div> <div class="form-group"> {!! Form::label('img_filename','Upload Image') !!} {!! Form::file('img_filename') !!} </div> {!! Form::hidden('status',$value='active') !!} </div><!-- /.box-body --> <div class="box-footer"> {!! Form::submit('Submit',$attributes=array('class'=>'btn btn-primary')) !!} </div> {!! Form::close() !!} 

This controller code is for saving the image correctly, but when I try to save the image file name in the table, this code stores the path to the database table.

Since I use the direct () method to store the query object in a table, I do not know how to save the file name instead of the path.

Check this image for table data.

+5
source share
2 answers

The problem is that your request data did not change when loading the image. So img_filename still contains tmpdata.

You can try the following:

 $input = $request->all(); $input['img_filename'] = $filename; 
+8
source

Code that works for me:

 $updir = 'images/'; $img_name = 'image.jpeg'; Request::file('img_filename')->move($updir, $img_name); 
+1
source

Source: https://habr.com/ru/post/1233793/


All Articles