Search code examples
phplaravelformsmultipart

'multipart/form-data' not working


I'm using Laravel and trying to build a gallery, i'm testing the upload of a file but i when i attach a file and click submit i can't get a positive outcome using the test set up. The code is below

GalleryController

// Store Gallery
    public function store(Request $request){
            // Get Request Input
        $name = $request->input ('name');        
        $description = $request->input ('description', '');
        $cover_image = $request->input ('cover_image');
        $owner_id = 1;

        // Check Image Upload
        if($cover_image){
die ('YES');
        } else {
die ('NO');
        }
    }

The form is set up as follows

{!! Form::open(array('action' => 'GalleryController@store', 'enctype' => 'multipart/form-data')) !!}
              {!! Form::label ('name', 'Name') !!}
              {!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Name', 'name' => 'name']) !!}

            {!! Form::label ('description', 'Description') !!}
              {!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Description', 'name' => 'Description']) !!}

            {!! Form::label ('cover_image', 'Cover Image') !!}
            {!! Form::file('cover_image') !!}

            {!! Form::submit ('Submit', $attributes = ['class' => 'button']) !!}
            {!! Form::close() !!}

Any help is appreciated Thanks


Solution

  • Your form looks correct, it is likely your controller where you are retrieving the uploaded file.

    As per the docs, to retrieve an uploaded file you should use $request->file():

     $request->file('cover_image');
    

    That link to the docs above also goes on to explain how you can check the file properly and store the file.