jbcaranay's avatar

Uploading several images

Hi! Im trying to upload several images in the same form. This is the function in PostController

public function store(CreatePostRequest $request)
{

    //
        $frontview = Input::file('frontview');
        $backview = Input::file('backview');
        $soleview = Input::file('soleview');
        $rightview = Input::file('rightview');
        $leftview = Input::file('leftview');
        $topview = Input::file('topview');
    //
        $frontviewname = $frontview->getClientOriginalName();
        $backviewname = $backview->getClientOriginalName();
        $soleviewname = $soleview->getClientOriginalName();
        $rightviewname = $rightview->getClientOriginalName();
        $leftviewname = $leftview->getClientOriginalName();
        $topviewname = $topview->getClientOriginalName();

    //Put image to /images/userposts
        Input::file('frontview')->move(public_path().'/images/userposts/', $frontviewname);
        Input::file('backview')->move(public_path().'/images/userposts/', $backviewname);
        Input::file('soleview')->move(public_path().'/images/userposts/', $soleviewname);
        Input::file('rightview')->move(public_path().'/images/userposts/', $rightviewname);
        Input::file('leftview')->move(public_path().'/images/userposts/', $leftviewname);
        Input::file('topview')->move(public_path().'/images/userposts/', $topviewname);
    //  
        $front['frontview'] = $frontviewname;
        $back['backview'] = $backviewname;
        $sole['soleview'] = $soleviewname;
        $right['rightview'] = $rightviewname;
        $left['leftview'] = $leftviewname;
        $top['topview'] = $topviewname;


    Post::create([
        'brands_id'     => $request['brands_id'],
        'sizes_id'      => $request['sizes_id'],
        'conditions_id' => $request['conditions_id'],
        'colors_id'     => $request['colors_id'],
        'shoe_types_id' => $request['shoe_types_id'],
        'locations_id'  => $request['locations_id'],
        'price'         => $request['price'],
        'description'   => $request['description'],
        'frontview'     => $front,
        'backview'      => $back,
        'soleview'      => $sole,
        'rightview'     => $right,
        'leftview'      => $left,
        'topview'       => $top
    ]);
}

I have no problem with the other inputs. Im only having trouble in the upload image part. I get this "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array" error when submitting. I appreciate it if you can help me out with this one.

0 likes
4 replies
SachinAgarwal's avatar
Level 21

I think it should be like this

    Post::create([
        'brands_id'     => $request['brands_id'],
        'sizes_id'      => $request['sizes_id'],
        'conditions_id' => $request['conditions_id'],
        'colors_id'     => $request['colors_id'],
        'shoe_types_id' => $request['shoe_types_id'],
        'locations_id'  => $request['locations_id'],
        'price'         => $request['price'],
        'description'   => $request['description'],
        'frontview'     => $front['frontview'],
        'backview'      => $back['backview'] ,
        'soleview'      => $sole['soleview'],
        'rightview'     => $right['rightview'],
        'leftview'      => $left['leftview'] ,
        'topview'       => $top['topview']

Please or to participate in this conversation.