Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimVanDijck's avatar

Can't upload a file in Laravel 5

I'm having a hard time getting file uploads to work in Laravel 5. When I try to get the file from the request and dump it, it returns "null".

Response:

null
array (size=3)
  '_token' => string 'lYgV8SPSOrcU7bYhQPL18Sggmvn7FGylD3AHpaL2' (length=40)
  'name' => string 'Test' (length=4)
  'template_file' => string '2-110P61AH80-L.jpg' (length=18)

Controller:

/**
     * Process the uploaded form.
     *
     * @Post("form/create")
     *
     * @return Response
     */
    public function postCreate(FormCreateRequest $request)
    {
        var_dump($request->file('template_file'));

        var_dump($request->all());

        return 'Posted';
    }

Request:

class FormCreateRequest extends Request {

    protected $dontFlash = ['template_file'];

    /**
     * Get the validation rules that apply to the request.
     *
     * @todo Validate mime-types.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'template_file' => 'required|max:1024',
            'javascript_file' => 'max:1024',
            'css_file' => 'max:1024'
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
} 
@extends('app')

@section('content')
<div class="container">
<div class="row">
    <div class="col-sm-8 col-sm-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Create a new form</div>
            <div class="panel-body">

                @include('partials.errors.basic')

                <form class="form-horizontal" role="form" method="POST" action="/form/create">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label">Name</label>
                        <div class="col-sm-6">
                            <input type="text" id="name" name="name" class="form-control" placeholder="Name" value="{{ old('name') }}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-3 control-label">Template</label>
                        <div class="col-sm-6">
                            <input type="file" id="template_file" name="template_file">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password" class="col-sm-3 control-label">CSS</label>
                        <div class="col-sm-6">
                            <input type="file" id="css_file" name="css_file">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password_confirmation" class="col-sm-3 control-label">Javascript</label>
                        <div class="col-sm-6">
                            <input type="file" id="js_file" name="js_file">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-3">
                            <button type="submit" class="btn btn-primary"><i class="fa fa-btn fa-cloud-upload"></i>Create Form</button>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>
</div>
@stop

Is this a bug or am I just missing something very obvious?

0 likes
8 replies
bestmomo's avatar

To upload files you must set this encryption in your form :

enctype="multipart/form-data"
13 likes
TimVanDijck's avatar

Ugh, basic mistake. Too busy looking at Laravel that I made a stupid error in my HTML.

Thanx for pointing that out!

2 likes
CarlosGarcell's avatar

@TimVanDijck Did the solution provided by @bestmomo worked for you? I am trying to upload 2 files, a PDF and an image, my form already has the enctype="multipart/formdata" attribute as I am creating the form this way {!! Form::open(['url' => 'signup/store', 'files' => 'true']) !!}. Any ideas? I tried removing the apostrophe from 'true' (like this->true), but I'm still getting null from dumping the value of the form field.

deevo's avatar

@TimVanDijck I just did the same on my "Edit" form. I sat here scratching my head for an hour trying to figure out why my files weren't passing and just realized how silly of a mistake it was.

ttson24's avatar

I have to this problem similar. client: i have form

@extends('admin.template.app')

@section('content')

<script src="{{ asset('/js/item.js') }}"></script>  
<script src="{{ asset('/js/common.js') }}"></script>
<script src="{{ asset('/resources/js/upload/vendor/jquery.ui.widget.js') }}"></script>
<script src="{{ asset('/resources/js/upload/tmpl.min.js') }}"></script>
<script src="{{ asset('/resources/js/upload/load-image.all.min.js') }}"></script>
<script src="{{ asset('/resources/js/upload/canvas-to-blob.min.js') }}"></script>
<script src="{{ asset('/resources/js/upload/jquery.blueimp-gallery.min.js') }}"></script>
<script src="{{ asset('/resources/js/upload/jquery.iframe-transport.js') }}"></script>
<script src="{{ asset('/resources/js/upload/jquery.fileupload.js') }}"></script>
<script src="{{ asset('/resources/js/upload/jquery.fileupload-process.js') }}"></script>
<script src="{{ asset('/resources/js/upload/jquery.fileupload-image.js') }}"></script>   
<script src="{{ asset('/resources/js/upload/jquery.fileupload-validate.js') }}"></script>   
<script src="{{ asset('/resources/js/upload/jquery.fileupload-ui.js') }}"></script>   
<script src="{{ asset('/resources/js/upload/main.js') }}"></script>   
<script src="{{ asset('/ckeditor/ckeditor.js') }}"></script>    
<div class="container full-screen">
    <div class="row">
        <div class="col-md-offset-0">
            @if (Session::has('msgCreate'))
                <div class="error alert-info">{{ Session::get('msgCreate') }}</div>
            @endif
            <div class="panel panel-default">
                {!!Form::token()!!}
                <div class="panel-heading upper-char">Create category item
                    <span class="item-right"><a href="{{ URL::to('admin/catitem')}}">List category item</a></span>
                </div>              
                <div class="panel-body">
                    {!!Form::open(['route'=>['admin.catitem.store'],'method'=>'post','id' => 'fileupload','files' => true])!!}
                        <div class="form-group">
                            {!!Form::label('name', 'Name')!!}
                            {!!Form::text('name', null,array('required','class' => 'form-control ','placeholder'=>'Enter category name','max-length'=>'50'))!!}
                        </div>
                        <div class="form-group">
                            {!!Form::label('parent', 'category item')!!}
                            {!! Form::select('parent',  array('0' => 'Please select') + $category, '', ['class' => 'form-control']) !!}
                        </div>
                        <div class="form-group">
                            {!!Form::label('detail', 'Detail')!!}
                            {!!Form::textArea('detail', null,array('required','class' => 'form-control','placeholder'=>'Enter detail information of category','max-length'=>'250'))!!}
                        </div>                         
                        <div class="form-group">
                            {!!Form::label('detail', 'Images')!!}                            
                            <div class="row fileupload-buttonbar">             
                                <div class="col-lg-6">
                                    <!-- The fileinput-button span is used to style the file input field as button -->
                                    <span class="btn btn-success fileinput-button"> <i
                                        class="glyphicon glyphicon-plus"></i> <span>Add files...</span> <input
                                        type="file" name="files[]" id="fileUp" multiple  >
                                    </span>
                                    <button type="button" class="btn btn-primary start " id="upload">
                                        <i class="glyphicon glyphicon-upload"></i> <span>Start upload</span>
                                    </button>
                                    <!-- The global file processing state -->
                                    <span class="fileupload-process"></span>
                                </div>
                                <!-- The global progress state -->
                                <div class="col-lg-5 fileupload-progress fade">
                                    <!-- The global progress bar -->
                                    <div class="progress progress-striped active" role="progressbar"
                                        aria-valuemin="0" aria-valuemax="100">
                                        <div class="progress-bar progress-bar-success" style="width: 0%;"></div>
                                    </div>
                                    <!-- The extended global progress state -->
                                    <div class="progress-extended">&nbsp;</div>
                                </div>
                            </div>
                            <!-- The table listing the files available for upload/download -->
                            <table role="presentation" class="table table-striped" >
                                <tbody class="files"></tbody>
                            </table>                           
                        </div>
                        
                        {!!Form::button('Create category item', array('class'=>'btn btn-default item-right', 'id'=>'addCategoryItem','type'=>'submit'))!!}
                    {!!Form::close()!!}
                </div>
                
            </div>
        </div>
    </div>
</div>
 <script type="text/javascript">
    CKEDITOR.replace( 'detail' ); 
 </script>
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">

{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-upload fade">
        <td>
            <span class="preview"></span>
        </td>
        <td>
            <p class="name">{%=file.name%}</p>
            <strong class="error text-danger"></strong>
        </td>
        <td>
            <p class="size">Processing...</p>
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
        </td>
        <td>
            {% if (!i && !o.options.autoUpload) { %}
                <button class="btn btn-primary start start-visible" disabled>
                    <i class="glyphicon glyphicon-upload"></i>
                    <span>Start</span>
                </button>
            {% } %}
            {% if (!i) { %}
                <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Cancel</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>
    <!-- The template to display files available for download -->
    <script id="template-download" type="text/x-tmpl">

{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        <td>
            <span class="preview">
                {% if (file.thumbnailUrl) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
                {% } %}
            </span>
        </td>
        <td>
            <p class="name">
                {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                {% } else { %}
                    <span>{%=file.name%}</span>
                {% } %}
            </p>
            {% if (file.error) { %}
                <div><span class="label label-danger">Error</span> {%=file.error%}</div>
            {% } %}
        </td>
        <td>
            <span class="size">{%=o.formatFileSize(file.size)%}</span>
        </td>
        
    </tr>
{% } %}
</script>
   
@endsection

i am submit button and it sent all data to server. but i do see value of file control var_dump(Input::all())

could you help me.

philkobby's avatar

File has been uploaded but cant find in the specified directory

slev1n's avatar

@philkobby If you used $request->file('file')->store() or File / Storage helpers, default directory is storage/app. But it's depends on current storage driver at config/filesystem.php.

If you just upload file via form, you need to save it into directory.

$request->file('file_field_name')->store('images')

Read about https://laravel.com/docs/master/filesystem

Please or to participate in this conversation.