php – Laravel 6: The file "xyz.jpg" was not uploaded due to an unknown error-ThrowExceptions

Exception or error:

I was making an array of forms that use for upload text and file (image).

Web Page
I have a problem with this. When I submit 1 form, everything is fine until I submit more than 1 form.

Here is the function for submitting form:

  public function addMorePost(Request $request)
{
    $request->validate([
        'addmore.*.name' => 'required',
        'addmore.*.qty' => 'required',
        'addmore.*.price' => 'required',
        'filenames.*' => 'required|image|mimes:jpeg,png,jpg|max:2048'
    ]);


    foreach ($request->addmore as $key => $value) {
        if($request->hasFile('filenames.*')) {
            $file = $request->file('filenames.*'); 
            foreach ($file as $files) {
                $fileName = $files->getClientOriginalName();
                $fileName = $fileName.'.'.$files->getClientOriginalExtension();
                $files->move(public_path(),$fileName);
            ProductStock::create(['name' => $request->addmore[$key]['name'],
                                'qty' => $request->addmore[$key]['qty'],
                                'price' => $request->addmore[$key]['price'],
                                'filenames' => $fileName
            ]);
        }
            }
      //  print_r($value);
    }
    return $request->addmore;
}

The problem is, when I submit 2 or more form (consider the 1st form has a file named xyz.jpg and abc.jpg for the 2nd form), then the error appears :

Laravel 6: The file “xyz.jpg” was not uploaded due to an unknown error.

The xyz.jpg was uploaded to public_path(), but not the abc.jpg

The error is on this syntax:

$files->move(public_path(),$fileName);

But I have no idea what is the right syntax should be.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *