Request Variable does not return input file

Asked

Viewed 1,412 times

2

Why can’t I get the View file?

I have a form that directs to a route. Among other attributes, I have an upload of files. This file upload is the only attribute not shown in request.

<label for="recipient" class="control-label">Foto:</label>
 <input id="foto" type="file" name="foto" accept="image/*" enctype="multipart/form-data">
 @if ($errors->has('foto'))
    <span class="help-block">
      <strong>{{ $errors->first('foto') }}</strong>
    </span>
 @endif
</div>

On the controller, I have:

dd($this->request->all());

This returns all attributes, but not even the 'photo'.

I’ve also tried:

dd($request->file('foto'));

What returns null even with a selected file.

  • in the form is it enabled to send email? p.x: {{ Form::open(array('url' => '/form', 'files' => true)) }}

  • Email or file? Thank you for the answer. I am using the normal html form tag without facade. What would HTML look like?

  • 1

    the enctype="multipart/form-data" is not in the input file is on the tag form, example, <form enctype="multipart/form-data" ....> an example of the form: https://www.w3schools.com/tags/att_form_enctype.asp

  • Yes, it is enabled in the form.

  • Using ajax to send php or Submit in form?

  • @Gabrielaugusto a check in the answer I posted, if still your problem is not solved, post your full form and the content obtained with the DD $request

Show 1 more comment

2 answers

6

Your input is correct except for the fact that enctype="multipart/form-data" this is an attribute of form and not of input

But with the form correct this does not interfere in sending

Thus, despite not having posted your form, the error must be on this or your route, to make a small test put on this form:

<form action="api/annex" method="post" enctype="multipart/form-data">

    <label for="recipient" class="control-label">Foto:</label>
    <input id="foto" type="file" name="foto" accept="image/*" enctype="multipart/form-data">
        @if ($errors->has('foto'))
            <span class="help-block">
            <strong>{{ $errors->first('foto') }}</strong>
            </span>
        @endif
    </div>

    <input type="submit">
</form>

And this worked correctly, as I demonstrate below:

Request {#39 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure {#123 ▶}
  #routeResolver: Closure {#124 ▶}
  +attributes: ParameterBag {#41 ▶}
  +request: ParameterBag {#40 ▶}
  +query: ParameterBag {#47 ▶}
  +server: ServerBag {#44 ▶}
  +files: FileBag {#43 ▼
    #parameters: array:1 [▼
      "foto" => UploadedFile {#28 ▼
        -test: false
        -originalName: "Dassad.png"
        -mimeType: "image/png"
        -size: 346505
        -error: 0
        path: "/tmp"
        filename: "phpJRjAf5"
        basename: "phpJRjAf5"
        pathname: "/tmp/phpJRjAf5"
        extension: ""
        realPath: "/tmp/phpJRjAf5"
        aTime: 2017-12-28 17:26:39
        mTime: 2017-12-28 17:26:39
        cTime: 2017-12-28 17:26:39
        inode: 1079759
        size: 346505
        perms: 0100600
        owner: 33
        group: 33
        type: "file"
        writable: true
        readable: true
        executable: false
        file: true
        dir: false
        link: false
      }
    ]
  }
  +cookies: ParameterBag {#42 ▶}
  +headers: HeaderBag {#45 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/api/annex"
  #requestUri: "/apiapp/public/api/annex"
  #baseUrl: "/apiapp/public"
  #basePath: null
  #method: "POST"
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isClientIpsValid: true
  -isForwardedValid: true
  basePath: "/apiapp/public"
  format: "html"
}

So check in your form if this with the attribute

enctype="multipart/form-data"

If it still doesn’t work check in your form if you have the correct method:

method="post"

If it still doesn’t work check your route if you have the correct method, e.g.:

Route::resource('annex', 'AnnexController', [
    'only' => [
            'store',
    ]
]);

or

Route::post('annex', 'AnnexController@store');

0

Maybe he’s using Ajax to send the data to the controller. In this case, as there is an input of type file ajax will not send the contents of it.

Use Formdata instead of serialize. below I show a code snippet in which I send data from a form that contains an input file type.

$('#btnAddProjecto').click(function () {
            var data = new FormData($('#myFormProjecto')[0]);// codigo antigo $('#myFormProjecto').serialize();
           
            $.ajax({
                type: 'jax',
                method: 'post',
                url: "{{ route('projecto.store')}}",
                data: data,
                processData: false,
                contentType: false,
                async: true,
                dataType: 'json',
                success: function (resposta) {
                    alert(resposta);
                },
                error: function (error) {
                    console.log(error);
                   
                }
            });
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.