cschoeni's avatar

Larave 5.2 - File Upload not work

When i upload a file. Give my with the dd($request->file('slim')); only null.

My Form

<form action="/photo/upload" files=true method="post">
    {!! Form::token() !!}
    <div class="slim" data-label="Drop your avatar here" data-size="240,240" data-ratio="1:1">
        <input type="file" name="slim" required />
    </div>
    </div> <!-- /.modal-body -->
        <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal">Abbrechen</button>
        <button type="submit" class="btn btn-primary">Speichern</button>
    </div>
</form>

My Controller

public function store(Request $request)
    {
        dd($request->file('slim'));
    }

My route

Route::post('/photo/upload', ['as' => 'store', 'uses' => 'Artist\ArtistPhotoController@store']);

The upload form I bought on http://preview.codecanyon.net/item/slim-image-upload-and-ratio-cropping-plugin/full_screen_preview/16364167?_ga=1.109654277.1615792691.1470001455

0 likes
8 replies
d3xt3r's avatar

enctype="multipart/form-data" add this to your form in order to be able to upload files ...

cschoeni's avatar

Hello @d3xt3r

I updated my Form header.

<form action="/photo/upload" files=true enctype="multipart/form-data"  method="post">

But even with the entry I receive "null"

d3xt3r's avatar

Oh, i see you are using some package, they probably do some form manipulation before its posted, you will have to resort to their methods in backend(php) as well.

dd($request->all()) ; to check what actually is being passed. with normal form it should look like this

array:1 [▼
  "slim" => UploadedFile {#145 ▶}
]

cschoeni's avatar

This i have, but i came many text more... When i make now with $request->file('slim') then i cam null...

array:2 [▼
  "_token" => "Gi3Uip2kiwWaEM3QojNg2AbUix5xjbAYNSyOrnjb"
  "slim" => "{"server":null,"meta":null,"input":{"name":"butterfly-wallpaper.jpeg","type":"image/jpeg","size":1584719,"width":2560,"height":1600},"output":{"width":240,"height":240,"image":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCADwAPADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NX
RonB1985's avatar

Just something I noticed, not a solution to your problem though, in your opening form you're using:

files=true

But that only works with the Form:: Facade, so like:

{{ Form::open(array('url' => route('store'), 'method' => 'post', 'files'=> true)) }}

And that will translate to:

<form action="/photo/upload"enctype="multipart/form-data"  method="post">
cschoeni's avatar

Hello RonB1985

Thanks for your message.

i change this with the Collective Form...

{{ Form::open(array('url' => route('artist.photo.store'), 'method' => 'post', 'files'=> true)) }}

My Form is now ...

<div class="modal-body">
    {{ Form::open(array('url' => route('artist.photo.store'), 'method' => 'post', 'files'=> true)) }}
        {!! Form::token() !!}
            <div class="slim" data-label="Drop your avatar here" data-size="240,240" data-ratio="1:1">
                <input type="file" name="slim[]" required />
            </div>
            </div> <!-- /.modal-body -->
            <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Abbrechen</button>
                  <button type="submit" class="btn btn-primary">Speichern</button>
            </div>
    {!! Form::close() !!}
</div>

Result with $request->all():

array:2 [▼
  "_token" => "Gi3Uip2kiwWaEM3QojNg2AbUix5xjbAYNSyOrnjb"
  "slim" => array:1 [▼
    0 => "{"server":null,"meta":null,"input":{"name":"butterfly-wallpaper.jpeg","type":"image/jpeg","size":1584719,"width":2560,"height":1600},"output":{"width":240,"height":240,"image":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQ.....

With $request->file('slim');

null

With $request->file('slim[]');

null
d3xt3r's avatar

Oh, i see you are using some package, they probably do some form manipulation before its posted, you will have to resort to their methods in backend(php) as well.

As i mentioned, if you are using a plugin, $request->file() may not work, and seeing the dd() of your request, it definitely wont. Check the package for the methods they have to access the file.

cschoeni's avatar

Thank you for your help. I can found the failure. I work for upload with slim upload (Project on CodeCanyon), the problem was the js file form this.

With the normal Code it works exactly.

<input type="file" name="slim[]" required />

Your answers led me to the idea

Please or to participate in this conversation.