@armancs Typo in your code:
$destinationPath = public_path('images\product');
Change it to:
$destinationPath = public_path('images/product');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to move image to public folder but its not store to that specific folder.
$data = new Product();
$data->fill($request->all());
$data->created_by_id = Auth::user()->id;
if($file = $request->file('product_image'))
{
$image_name = time() . '.' . $file->getClientOriginalExtension();
$destinationPath = public_path('images\product');
$file->move($destinationPath,$image_name);
$data['product_image'] = $image_name;
}
$data->save();
return \redirect()->back();
Solved this problem.
I forgot to add enctype="multipart/form-data" in my form.
when upload an image or file enctype should be include in form.
and this is my final code for controller.
$this->validate($request,[
'product_image' => 'mimes:jpeg,jpg,png,gif|required|max:10000'
]);
if($request->hasFile('product_image'))
{
$image_name = $request->file('product_image')->getClientOriginalName();
$filename = pathinfo($image_name,PATHINFO_FILENAME);
$image_ext = $request->file('product_image')->getClientOriginalExtension();
$fileNameToStore = $filename.'-'.time().'.'.$image_ext;
$path = $request->file('product_image')->storeAs('public/product',$fileNameToStore);
}
else{
$fileNameToStore = 'noimage.jpg';
}
$data = new Product();
$data->fill($request->all());
$data->created_by_id = Auth::user()->id;
$data->product_image = $fileNameToStore;
$data->save();
return \redirect()->back();
And for view:
just added enctype.
<form class="needs-validation" novalidate method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
THanks everyone for your effort.
Please or to participate in this conversation.