0

i want to upload multiple images and store them into database, but i got an error like this :

file_get_contents() expects parameter 1 to be a valid path, array given

This is my controller :

public function fileMultiple(Request $request) {
        $this->validate($request, [
            'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ]);

        if($request->hasfile('filename'))
         {

            foreach($request->file('filename') as $image)
            {
                $name=$image->getClientOriginalName();
                $image_encod = base64_encode(file_get_contents($request->file('filename')));
                $destinationPath = public_path('/images');
                $image->move($destinationPath, $name);

                $data = new Image();
                $data->image_name = $image_encod;
                $data->save();

            }
         }

        return back()->with('success', 'Your images has been successfully');
    }

how to fix it, the image must encode using base64

3 Answers 3

4

You can simply change a little bit in foreach loop and use the $key by following:

foreach($request->file('filename') as $key => $image)
{
   $name=$image->getClientOriginalName();
   $image_encod = base64_encode(file_get_contents($request->file('filename')[$key]));
   $destinationPath = public_path('/images');
   $image->move($destinationPath, $name);

   $data = new Image();
   $data->image_name = $image_encod;
   $data->save();

}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you're sending the array value.

The following code:

$image_encod = base64_encode(file_get_contents($request->file('filename')));

should be changed into:

$image_encod = base64_encode(file_get_contents($image));

1 Comment

Welcome to Stack Overflow!
0
<?php 

public function fileMultiple(Request $request) {

    $this->validate($request, [
        'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    if(is_array($request->filename) && count($request->filename) > 0){

        foreach ($request->filename as $key => $file) {

            if($request->hasFile('filename.' . $key)){

                $file = $request->file('filename.' . $key);

                if($file->store(public_path('/images')))
                {
                    $data = new Image();
                    $data->image_name = $image_encod;
                    $data->save();

                    return back()->with('success', 'Your images has been successfully');
                }
                else{
                    throw new \Exception('Unable to save image.');
                }
            }
        }
    }

    return back()->with('error', 'Unable to save image.');
}

2 Comments

i have tried your code, and i got error Impossible to create the root directory
Can you check file permissions of public folder?

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.