david001's avatar

Image orginal name not saved in database

I have a resourceful routing and i have controller like this

public function store(){

  if($request->hasFile('file')){

            $file = $request->file('file');
            $destinationPath = public_path().'/files';
           $filename = date("Y-M-D").$file->getClientOriginalName();
            $file->move($destinationPath, $filename);
        }

  if($request->hasFile('file')){

            $file = $request->file('file2');
            $destinationPath = public_path().'/files';
        $filename = date("Y-M-D").$file->getClientOriginalName();
            $file->move($destinationPath, $filename);
        }
$member = $this->member->create($request->all());
return redirect::back();
}

on dd($request->all());i got image orginal name but when i looked at database there is no image orginal name insted i got /tmp/php9b6Zo0in place of image name

"file" => UploadedFile {#30 ▼
    -test: false
    -originalName: "imagesww.jpeg"
    -mimeType: "image/jpeg"
    -size: 6257
    -error: 0
  }

  "file2" => UploadedFile {#31 ▼
    -test: false
    -originalName: "php-4.jpg"
    -mimeType: "image/jpeg"
    -size: 30175
    -error: 0
  }```

What may be error ,how can i save image orginal image name in database
0 likes
5 replies
Snapey's avatar

in your second request->hasFile it says file again instead of file2

you are not getting the original name into something that can be inserted into the database?

what are the column names for file and file2 in your model

david001's avatar

column names for file and file2 in my model

Snapey's avatar
Snapey
Best Answer
Level 122

You need to get the filenames before calling the save;

public function store(){

    $destinationPath = public_path().'/files';

    $data = $request->all();

    if($request->hasFile('file')){

        $file = $request->file('file');
        $data['file'] = date("Y-M-D").$file->getClientOriginalName();
        $file->move($destinationPath, $data['file']);
    }

    if($request->hasFile('file2')){
        
        $file = $request->file('file2');
        $data['file2'] = date("Y-M-D").$file->getClientOriginalName();
        $file->move($destinationPath, $data['file2']);
    }

    $member = $this->member->create($data);
    return redirect::back();
}

1 like
shez1983's avatar

instead of having two if statements.. you can move this logic to a dedicated class.. OR have a foreach statement...

Please or to participate in this conversation.