Laracast13's avatar

Upload Multiple Images with multidimensional array

Hello

Tying create Upload Multiple Images with multidimensional array

DB

        Schema::create('galleries', function (Blueprint $table) {
            $table->id();
            $table->string('category')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();
        });
<input type="file" id="image" name="image[]" class="form-control" multiple>

controller

      $path = "upload/";

        foreach ($image as $multi_img) {
        $name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
        Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
        $last_img = $path.$name_gen;
        Gallery::insert([ 
             'image'=> $last_img,
         ]);
        }

This works and it uploads image.

But I want make like

<input type="file" id="image" name="image[1][]" class="form-control" multiple>
<input type="file" id="image" name="image[2][]" class="form-control" multiple>
<input type="file" id="image" name="image[3][]" class="form-control" multiple>

How make for each for this image[X][]

If file uploaded from image[1][] in 'category'=> 1

      $path = "upload/";

        foreach ($image as $multi_img) {
        $name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
        Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
        $last_img = $path.$name_gen;
       Gallery::insert([ 
             'image'=> $last_img,
             'category'=> ???,
         ]);
        }
0 likes
9 replies
mabdullahsari's avatar

Your question is not clear. Nobody will be able to help you in this case.

trin's avatar
$path = "upload/";

foreach ($image AS $category => $images) {
	foreach ($images as $multi_img) {
		$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
		Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
		$last_img = $path.$name_gen;
		Gallery::insert([ 
			'image'=> $last_img,
			'category' => $category
		]);
	}
}
1 like
trin's avatar

and, of couse, use $request->input('image') or $request->validate('image'), not $image direct

1 like
Laracast13's avatar

Hello This is full code in my function , but getting error "The image.1 must be a file of type: jpeg, jpg, png, gif."


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

$image = $request->file('image'); 

$path = "upload/";

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

foreach ($image AS $category => $images) {
	foreach ($images as $multi_img) {
		$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
		Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
		$last_img = $path.$name_gen;
		Gallery::insert([ 
			'image'=> $last_img,
			'category' => $category
		]);
	}
}
}
Laracast13's avatar

After testing find problem 1. This validation not works, how can it work for multiply image?

 'image.*' => 'mimes:jpeg,jpg,png,gif|max:2048'

I change with this, correct ?

'image.*.*' => 'mimes:jpeg,jpg,png,gif|max:2048'

if i use this check, upload not works

if($request->hasfile('image')) {
trin's avatar
trin
Best Answer
Level 5

because image not are array of image now, its array of category. try to:

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

$path = "upload/";

foreach ($request->input('image') AS $category => $image) {
	foreach ($request->file('image')[$category] as $multi_img) {
		$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
		Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
		$last_img = $path.$name_gen;
		Gallery::insert([ 
			'image'=> $last_img,
			'category' => $category
		]);
	}
}
1 like
trin's avatar
if($request->hasfile('image')) {

redundant check

1 like
Laracast13's avatar

It is not correct use ($image = $request->file('image'); ) this also works

$image = $request->file('image'); 

foreach ($image AS $category => $images) {
	foreach ($images as $multi_img) {
		$name_gen = hexdec(uniqid()).'.'.$multi_img->getClientOriginalExtension();
		Image::make($multi_img)->resize(300,300)->save($path.$name_gen);
		$last_img = $path.$name_gen;
		Gallery::insert([ 
			'image'=> $last_img,
			'category' => $category
		]);
	}
}

or it must be like you show

foreach ($request->input('image') AS $category => $image) {

Please or to participate in this conversation.