Why is my image not storing in a specified folder in Laravel 5.6?


Why is my image not storing in a specified folder in Laravel 5.6?



In my form, I asked for an image to upload. Then I already validated it and it works. But the file is not stored in the uploads folder.



Here's a snippet of my ProductController:


public function store(Request $request)
{
// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);

// Upload image
if($request->hasFile('image')) {
app()->make('path.public/uploads');
$image = $request->image;

$image->move('uploads', $image->getClientOriginalName());


}

/*// Save the data into database
Product::create([
'name' => $request->product_name,
'price' => $request->product_price,
'description' => $request->product_desc,
'image' => $request->image->getClientOriginalName()
]);


// Echo a session message
$request->session()->flash('msg', 'Your product has been added');


// Redirect to view page
return redirect('/products');*/

}



I already tried looking at other possible solutions but the other questions were already at storing the image in the database. I also tried checking if uploads was a directory and existed, and it is.


uploads



Can anyone please help? Thanks.




3 Answers
3



Try this: official documentation: This is how it should look in your controller:



if($request->hasFile('image')) {
$request->file('image')->store('uplodads/', 'public');
}


if($request->hasFile('image')) {
$request->file('image')->store('uplodads/', 'public');
}



This would store the image in /storage/app/public/uploads by default. You can also change the public path in /config/filesystems.php. You can then access the file (if you linked the storage) with asset('storage/uploads'.$img_name).


/storage/app/public/uploads


public


/config/filesystems.php


asset('storage/uploads'.$img_name)





I didn't change yet the public path, because I wanted only function store() to use the uploads folder. Regarding the code, I have tried it and change my controller, but the image won't still save.
– Francis Albert Calas
Jun 30 at 14:18



public





Did the uploads folder appeared in storage/app/public? Also the app has enabled debugging and doesn't throw any error? Also make sure you using IlluminateHttpRequest; not the Request facade.
– Levente Berky
Jun 30 at 17:35


uploads


storage/app/public


IlluminateHttpRequest;


Request


app()->make('path.public/uploads');





I've tried it, but it doesn't work.
– Francis Albert Calas
Jun 30 at 14:24



I've already solved it. The variable was wrong all along. Instead of it being product_img, I placed image.


product_img


image



Here's the updated code:


// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);

// Upload image
if($request->hasFile('product_img')) {
$image = $request->product_img;
$image->move('uploads', $image->getClientOriginalName());
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter