Multiple Image Upload?
Hi
I have upload multiple image in laravel.
But only one image upload on server.
How to upload multiple image?
if($request->hasfile('files')) {
$image_upload = $request->file('files');
// dd($image_upload);
foreach($request->file('files') as $file)
{
$mextention = $file->getClientOriginalName();
if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
Flash::error('This Image Already Uploaded');
return redirect()->back();
}
else
{
$image_example[] = $mextention;
$file->move('uploads/product_images', $mextention);
MultipleImage::insert( [
'images'=> $mextention,
'image_type' => 'product_image',
'active' => 1,
]);
// Flash::success('Product Image uploaded successfully');
// return redirect()->back();
}
dd($image_example);
}
}
Have you added attribute enctype="multipart/form-data" in form tag
and input tag as:
<input type="file" name="files[]" multiple />
@Vijay
That is all correct..But only one value upload on my DB
@Kesavan_Kani The problem is
dd($image_example);
Remove about code.
@Vijay I am remove the code but save only one file..But i have upload 3 file
@Kesavan_Kani
return redirect()->back();
Above code should be placed outside the foreach loop
You are either returning or doing dd() inside the foreach, meaning it stops at the first one.
Try
$image_upload = $request->file('files');
dd($image_upload);
@Sinnbeck i have upload 3 image..But only one save the server
if($request->hasfile('files')) {
$image_upload = $request->file('files');
foreach($request->file('files') as $file)
{
$mextention = $file->getClientOriginalName();
if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
Flash::error('This Image Already Uploaded');
return redirect()->back();
}
else
{
$image_example[] = $mextention;
$file->move('uploads/product_images', $mextention);
MultipleImage::insert( [
'images'=> $mextention,
'image_type' => 'product_image',
'active' => 1,
]);
Flash::success('Product Image uploaded successfully');
return redirect()->back();
}
// dd($image_example);
}
}
@Kesavan_Kani Of course not, you can't have the return inside the loop, it will return on first itteration.
You need to place it out side the loop.
if($request->hasfile('files')) {
$image_upload = $request->file('files');
foreach($request->file('files') as $file)
{
$mextention = $file->getClientOriginalName();
if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
Flash::error('This Image Already Uploaded');
return redirect()->back();
}
else
{
$image_example[] = $mextention;
$file->move('uploads/product_images', $mextention);
MultipleImage::insert( [
'images'=> $mextention,
'image_type' => 'product_image',
'active' => 1,
]);
}
// dd($image_example);
}
Flash::success('Product Image uploaded successfully');
return redirect()->back();
}
@Kesavan_Kani Can you please try my code and tell me the output? Is it 3 files?
@Sinnbeck Yes is it 3 files... But save only one in DB
@Tray2
Hi..same error... only one file upload in the db...not all images
@Kesavan_Kani And are you absolutely sure you dont hit the "This Image Already Uploaded" ? If you do, it wont handle any other images.
Or show your current code after changing it.
Please or to participate in this conversation.