So I am making a CRUD application where I want to store data from the user. But I want to store all images to a directory called images and the name of them to the data base.
This is my store method:
public function store(Request $request)
{
//
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('image'))
{
$name = $file->getClientOriginalName();
$file->move('images', $name);
$input['image'] = $name;
}
$user->posts()->create($input);
return redirect('/posts');
}
So you can see the method move() there, that will go to the public directory and check if there is an image directory if not, make one and store the image with their original name.
When I create, everything is ok. But my images are not saving in a directory!
This is my database, just in case u need it but here everything is ok:
Now when i go to the public there is no image directory created and no photo stored!
CodePudding user response:
Try this it work fine for me:
if ($request->file('image')) {
$image_name = "";
$file = $request->file('image');
$image_name = time() . rand(1, 100) . '.' . $file->extension();
$file->move(public_path('/images'), $image_name);
$input['image'] = $image_name;
}
CodePudding user response:
You need use this code is work.
if($request->file('image') != "")
{
$attachment = $request->file('image');
$new_name = rand() . '.' . $attachment->getClientOriginalExtension();
$attachment->move(public_path('images/products/'), $new_name);
}