lucassimines's avatar

Problem with image upload using array (name="img[]")

Hello guys,

I'm facing a problem when trying to upload an image in a repeating field, here is a part of my form:


{!! Form::label('name','Image') !!}
{!! Form::file('img[]', null, ['class'=>'form-control']) !!}

Here are my casts:

protected $casts = [
        'title' => 'array',
        'desc' => 'array',
        'img' => 'array',
    ];
}

Here is my Store method:

public function store(Request $request)
{

    $files = Input::file('img');
    foreach($files as $file) {
        $destinationPath = public_path('/images/uploads/');
        $filename = $file->getClientOriginalName();
        $file->move($destinationPath, $filename);
        $test['img'] = $filename;
    }

    $this->test->create($request->all());
    return redirect()->route('admin.tests');
}

I can make the Title and Desc work but when trying to upload an image I get this:


App\Test {#726
         id: "15",
         title: "["test1"]",
         desc: "["test2"]",
         img: "[{}]",
         created_at: "2016-03-29 03:16:39",
         updated_at: "2016-03-29 03:16:39",
       },

How can my img column receive the path instead of '{}' only?

Thanks!

0 likes
8 replies
JackD's avatar

use this to get the image filename

$request->file('img')
lucassimines's avatar

@JackD I get the following error
strrpos() expects parameter 1 to be string, array given
when replacing the

$filename = $file->getClientOriginalName();
for
$filename = $request->file('img');

JackD's avatar

Add dd($files); in your store method to check if it gets something from the image file input

lucassimines's avatar

Yea, it gets the following:

array:1 [▼
  0 => UploadedFile {#212 ▼
    -test: false
    -originalName: "IMG_3163.jpg"
    -mimeType: "image/jpeg"
    -size: 1613049
    -error: 0
    path: "/private/var/folders/yp/336b1bzn14n6yfrdxq4r34fm0000gn/T"
    filename: "phpL8M9mO"
    basename: "phpL8M9mO"
    pathname: "/private/var/folders/yp/336b1bzn14n6yfrdxq4r34fm0000gn/T/phpL8M9mO"
    extension: ""
    realPath: false
    writable: false
    readable: false
    executable: false
    file: false
    dir: false
    link: false
  }
]
JackD's avatar

Check your create method also if the img is properly referenced to it

JackD's avatar

If it is properly referenced to your create method check the responsible model for the image and check if the "img" is included inside protected fillable

lucassimines's avatar

Yea it is inside the $fillable.

Well, I created the $test and got the image file name inside my array, like:

App\Test {#694
         id: "4",
         title: "["my title"]",
         desc: "["my desc"]",
         img: ""img_test.jpg"",
         created_at: "2016-03-29 14:04:06",
         updated_at: "2016-03-29 14:04:06",
       },

But I get the following error now when calling the img on my index:

htmlentities() expects parameter 1 to be string, array given
lucassimines's avatar

My bad, got it.

It was trying to convert the old imgs with arrays inside when it was wrong, like: img: {[]}, so I deleted them and made it work.

But now I got another problem, it is storing just the last image into my array img:

Like:


App\Test {#677
         id: "9",
         title: "["teste","teste2"]",
         desc: "["teste","teste2"]",
         img: ""img2.jpg"",
         created_at: "2016-03-29 14:21:38",
         updated_at: "2016-03-29 14:21:38",
       },

It was supposed to be:

App\Test {#677
         id: "9",
         title: "["teste","teste2"]",
         desc: "["teste","teste2"]",
         img: "["img1.jpg","img2.jpg"]",
         created_at: "2016-03-29 14:21:38",
         updated_at: "2016-03-29 14:21:38",
       },

Please or to participate in this conversation.