Asim
Asim

Reputation: 452

Laravel Input::hasFile returning false while Input::file() gives all the information about file

I have written following basic file upload code which works perfectly on localhost. On live version, Input::hasFile('profile_picture') returns false consequently file is not moved to the required directory, while $filename = Input::file('profile_picture')->getClientOriginalName(); works fine and receives the file name.

On removing the if hasFile check before moving the file, the move function throws exception saying, Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException File could not be uploaded: missing temporary directory.

What I have confirmed:

upload.blade.php contains:

{{ Form::open(['url' => 'api/users', 'files' => true]) }}
File: {{ Form::file('profile_picture') }}
{{ Form::hidden('email', '[email protected]') }}
{{ Form::submit('send') }}
{{ Form::close() }}

Generated form looks like:

<form method="POST" action="http://localhost:8000/api/users" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="96OKRdiwnHlku0uMLYYpHPjAIKAiqoKRp372fUWc">
File: <input name="profile_picture" type="file">
<input name="email" type="hidden" value="[email protected]">
<input type="submit" value="send">
</form>

routes.php contains:

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

Route::resource('api/users', 'UsersController');

UsersController::store()

    public function store()
    {
        if ($this->user->isValid($data = Input::all())) {

            $data = Input::all();
            $filename = Input::file('profile_picture')->getClientOriginalName();
            $data['profile_picture'] = $filename;

            if (Input::hasFile('profile_picture')) {
                Input::file('profile_picture')->move(public_path('pictures/profile'), $filename);
            } else {
                return "Input::hasFile('profile_picture') has returned false. Size = "
                    . Input::file('profile_picture')->getClientSize();
            }

            $this->user->fill($data);
            $this->user->save();
            return Response::json($this->user);
        }

        return Response::json($this->user->errors);
    }

Upvotes: 2

Views: 2952

Answers (1)

tomprouvost
tomprouvost

Reputation: 115

Look's like a permission's problem on the server.

try this php function to know what is the path for the temporary files

ini_get('upload_tmp_dir');

Then, check the permissions of the folder.

Upvotes: 2

Related Questions