1

Im trying to upload multiple images to via below code in laravel. In my form there are 3 types of images to be selected to upload. When user select all the images and then submit the form. I need to upload all the images to same folder . First images get uploaded in to the folder. But then it gives me below error.

The file "1575738164-main-slider2.webp" was not uploaded due to an unknown error.

Controller

  if ($request->hasFile('image') &&  $request->hasFile('image_575') && $request->hasFile('image_768')){

        $file = $request->image;
        $file_575 = $request->image_575;
        $file_768 = $request->image_768;

        $name = time().'-'.$file->getClientOriginalName();
        $name_575 = time().'-'.$file_575->getClientOriginalName();
        $name_768 = time().'-'.$file_768->getClientOriginalName();

        $names = [ $name , $name_575 , $name_768];

        foreach ( $names as $n){

            $file->move('uploads/banners/',$n);   
        }

        $banner = new Banner();
        $banner->name = $name;
        $banner->name_575 = $name_575;
        $banner->name_768 = $name_768;

        $banner -> side_color = $request -> side_color ;
        $banner->type = $request->type;
        $banner->save();
    }

Please note that I have almost gone through below questions. Laravel: The file was not uploaded due to an unknown error

2 Answers 2

1

First only using time() method won't work to generate unique file name for all three images all the time and when a concurrent request occurs.

Second:

    $names = [ $name , $name_575 , $name_768];

    foreach ( $names as $n){

        $file->move('uploads/banners/',$n);   
    }

What you are looping is totally wrong. You are trying to move the same image, $file for three times.

You have to move all the three images inside the loop: `

    $file = $request->image;
    $file_575 = $request->image_575;
    $file_768 = $request->image_768;

`

So, you should probably do:

 $filesToMoves = [$name=> $file, $name_575 => $file2 , $name_768 => $file3];

foreach($filesToMoves as $fileName => $fileToMove){
        $fileToMove->move('uploads/banners/',$fileName);  
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the comment. Yes I know files wont be samed in names. However Im having a new error Illegal offset type now
I have updated my answer please check. I forgot to change the $file in the loop to $fileToMove
Also on the $request make sure that you are receiving image, image_575 and image_768
Thanks yes they are receving via request. I have changed your code with $ name_768 but still it generate error iIllegal offset type
Sorry for the late reply. Looks like the array I build was incorrect, I made the class object as the array index so it was throwing Illegal offset type. I have changed the code above.
|
0

I will add my code for future references that I used to solve this issue

public function store(Request $request)
{

    $this -> validate ( request () , [
        'image' => 'required|mimes:webp|dimensions:max_width=1200,max_height=380|max:50' ,
        'image_575' => 'required|mimes:jpeg,png,jpg|dimensions:max_width=575,max_height=380|max:80' ,
        'image_768' => 'required|mimes:jpeg,png,jpg|dimensions:max_width=768,max_height=380|max:80' ,
    ] ) ;

    if ($request->hasFile('image') &&  $request->hasFile('image_575') && $request->hasFile('image_768')){

        $fils = [$request->image, $request->image_575, $request->image_768];
        $formats = ['webp' , '575','768'];
        $fileNames = [];
        $i = 0;
            foreach($fils as $file){
                    $name = time().'_'.$formats[$i].'.'.$file->getClientOriginalExtension();
                    $file->move('uploads/banners/', $name);
                    array_push($fileNames, $name);
                    $i++;
            }

        $a= new X();
        $a->name = $fileNames[0];
        $a->image_575 = $fileNames[1];
        $a->image_768 = $fileNames[2];

        $a->save();
    }

This is just for information.

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.