1

I want to upload multiple images at a time, but it doesn't work.

Blade/View

<form action="{{ url('admin/image/upload') }}" method="POST"enctype="multipart/form-data">
@csrf
    <td><input type="text" name="Fields[0][title]" placeholder="Enter title" class="form-control" /></td>  
    <td><input type="file" name="Fields[0][image]" class="form-control" /></td>
</form>

Controller

$request->validate([
            'Fields.*.title' => 'required|max:25|min:5',
            'Fields.*.image' => 'required',
        ]);
           if($request->hasFile('image'))
            {
            $file=$request->file('image');
            $file_name=hexdec(uniqid());
            $ext=strtolower($file->getClientOriginalExtension());
            $file_full_name=$file_name.'.'.$ext;
            $upload_path='image/';
            $file_url=$upload_path.$file_full_name;
            $success=$file->move($upload_path,$file_full_name);
        $Fields['image']=$file_url;
       foreach ($request->Fields as $key => $value) {
            images::create($value);
        }
     
         return back()->with('success', 'Image Has Been Saved Successfully.');
}
5
  • check this line if($request->hasFile('image')), you don't have any filed called image Commented Feb 10, 2021 at 5:48
  • What should I write? Commented Feb 10, 2021 at 5:51
  • I think you dont need dynamic field here, change name="Fields[0][image]" to name="image" Commented Feb 10, 2021 at 5:52
  • I need dynamic field. Commented Feb 10, 2021 at 5:57
  • If you take my answer, you will get array of image and Fields input and you should access throgh "foreach ($request->file('image') as $img) { echo $img }" Commented Feb 10, 2021 at 6:08

2 Answers 2

1

Please follow below answer:

blade file

<form action="{{ url('admin/image/upload') }}" method="POST"enctype="multipart/form-data">
@csrf
    <td><input type="text" name="Fields[]" placeholder="Enter title" class="form-control" multiple /></td>  
    <td><input type="file" name="image[]" class="form-control" multiple /></td>
</form>

comtroller

$request->validate([
    'Fields.*' => 'required|max:25|min:5',
    'image.*' => 'required',
]);
   if($request->hasFile('image'))
    {
        // dd($request->image) 
foreach ($request->image as $file) {

    dd($file)   
    //get filename with extension
    $filenamewithextension = $file->getClientOriginalName();
    //get filename without extension
    $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
    //get file extension
    $extension = $file->getClientOriginalExtension();
    //filename to store
    $filenametostore = $filename . '_' . time() . '.' . $extension;
}

 return back()->with('success', 'Image Has Been Saved Successfully.');
}
Sign up to request clarification or add additional context in comments.

8 Comments

I get an error " Call to a member function getClientOriginalExtension() on array "
Test by dd() then go ahead step by step
I was copy and paste this.
copy, past now and test by dd()
do not you get any data in dd() function?
|
0

I hope this solution helps.(This is for image upload, but you can just delete the image validations)

The first thing is in the form, the field should be like this.

<input type="file" name="image_files[]" id="image_files" multiple accept="image/*" />

then on your Controller

public function uploadFiles(Request $request){
       
        $validatedData = $request->validate([
            'image_files' => 'required',
            'image_files.*' => 'mimes:gif,png,jpg,jpeg,svg'
        ]);
        
        if($request->hasfile('image_files'))
        {
            foreach($request->file('image_files') as $key => $file)
            {
                $file->move(storage_path('/products/images'),$file->getClientOriginalName());
            }
        }
        
        return "Files uploaded";

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.