phplaravellaravel-4

Using Input::all() when uploading files in laravel 4.2


According to this, if you do the following

<?php

// app/routes.php

Route::get('/', function()
{
    return View::make('form');
});

Route::post('handle-form', function()
{
    var_dump(Input::all());
});

We'll get the following:

array(0) { }

According to Dayle Rees, that is because files are stored in the $_FILES array and not in $_GET or $_POST. So when we change the second function to:

Route::post('handle-form', function()
{
    var_dump(Input::file('book'));
});

We get:

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) {<
  ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=><
  bool(false)<
  ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=><
  string(14) "codebright.pdf"<
  ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=><
  string(15) "application/pdf"<
  ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=><
  int(2370413)<
  ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=><
  int(0)<
  ["pathName":"SplFileInfo":private]=><
  string(36) "/Applications/MAMP/tmp/php/phpPOb0vX"<
  ["fileName":"SplFileInfo":private]=><
  string(9) "phpPOb0vX"<
}<

However, in my project, when I use Input::all(), I still get the correct output very identical to the above. The file I used is different, but I hope you get the point. Why is my projects giving different outputs from the book?


Solution

  • If you see into /vendor/laravel/framework/src/Illuminate/Http/Request.php,

    /**
     * Get all of the input and files for the request.
     *
     * @return array
     */
    public function all()
    {
        return array_replace_recursive($this->input(), $this->files->all());
    }
    

    which contains both files and other inputs. Since CodeBright was started with laravel 3, I suppose (but not sure), this section of code was updated later on Laravel 4.