简体   繁体   中英

laravel 5 multiple upload and store their name into database

i am currently following this answer and here is my code

public function add(Request $request) {
$files=[];
if($request->file('remind_letter')) $files[]=$request->file('file1');
if($request->file('answer_note')) $files[]=$request->file('file2');
if($request->file('negotiation')) $files[]=$request->file('file3');
foreach($files as $file){
if(!empty($file){
$filename=$file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($filename.'.'.$extension,
                                         File::get($filename));
$entry = new Docks();
$entry->div = $request->input('div');
$entry->year = $request->input('year');
$entry->activity = $request->input('activity');
$entry->corporate = $request->input('corporate');
--- need logic to save their path into databse ----
$entry->save

and i am still confuse how to save their path into database it is should be like this ?

$entry->remind_letter = $file->getFilename().'.'.$extension
$entry->answer_note= $file->getFilename().'.'.$extension
$entry->negotiation = $file->getFilename().'.'.$extension

You can assign filename using only $filename=$file->getClientOriginalName(); This will give you full file name included original extension.

Let's assume you want to upload your files into the folder '/public/files/'.

I suggest you use this inside your foreach:

if( !empty($file) )
{
    $file->move(base_path() . '/public/files/', $file->getClientOriginalName());
}

then for store filepath inside database ( I'm using filepath property as example):

$entry->filepath = base_path() . '/public/files/' . $file->getClientOriginalName();

In this way you will save the file where you desire and then have a full path to your file stored inside database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM