user0111001101
user0111001101

Reputation: 187

laravel 5.2 store image inside database

my controller is:

public function store(Request $request)
{
  $this->validate($request, ['title' => 'required',
                             'date' => 'required',
                             'image_1' => 'mimes:png,jpeg',
                            ]);

  $user = Auth::user()->id;
  $report = new Report($request->all());
  $report->author_id = $user;

  $image = $request->file('image_1');
  $ext = $image->getClientOriginalExtension();
  $path = public_path('uploads/reports/' . $image . $ext);

  $report->image_1 = $path;
  $report->save();

  Session::flash('flash_message', 'Report added!');

  return redirect('dash/reports');
}

all ok without name of my image, my app store data like

/var/www/localhost/htdocs/gemini/public/uploads/reports//tmp/phpul0PNipng

why use /tmp/randomstring name?

Upvotes: 0

Views: 399

Answers (1)

Tadas Paplauskas
Tadas Paplauskas

Reputation: 1903

$request->file('image_1')

This returns an object, not a filename. You get extension correctly, so you can get the filename the same way:

$path = public_path('uploads/reports/' . $image->getClientOriginalName() . $ext);

Upvotes: 2

Related Questions