Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input::file() returning null Laravel

I've been writing an uploading script and even though I'm using the Laravel built in function Input::file() it still returns null. I am going to post my Home Controller code.

public function handleUpload()
    {
        $user = Auth::user();
        $username = $user->username;
        $userId = $user->id;
        $path_to_users = "users";
        $user_profile_img = 'users/'.$username.$userId.'/'.$username.'image001.jpg';
        $user_path_profile = "users/$username$userId";
        $user_image_name = $username.'image001.jpg';
        if(file_exists($path_to_users))
        {
            if(file_exists("$user_path_profile")){
                $file = Input::file('profile')->move("$user_path_profile/", $user_image_name);
            } else {
            mkdir("$user_path_profile");
            $file = Input::file('profile')->move("$user_path_profile/", $user_image_name);
        }

        } else {
            mkdir("users");
            mkdir("$user_path_profile");
            $file = Input::file('profile')->move("$user_path_profile/", $user_image_name);
        }
    }

When I die(var_dump(Input::file('profile'))) returns null which means it is basically not taking in my file as an actual file. When I do die(var_dump(Input::get('profile'))) it returns the image name meaning that my uploading script is working but the Laravel backend is failing. Is this true, or am I just missing something?

Here is my HTML form stuff:

@if($user->id == $userProf->id)

                            <div class="fileUpload button-success pure-button">  
                                <span>Upload</span>
                                <form action="{{action('HomeController@handleUpload')}}" method="POST" enctype="multipart/form-data">
                                    <input type="file" name="profile" class="upload" />

                                </form>
                            </div>
                            @endif
like image 560
noahdotgansallo Avatar asked Mar 19 '14 17:03

noahdotgansallo


3 Answers

I had the same problem in a normal form. I forgot to add:

enctype="multipart/form-data" 
like image 183
DenLanden Avatar answered Oct 01 '22 20:10

DenLanden


I had the same issue and realised I had left out something in the form. Use the built in laravel forms as it will automatically add CRSF protection (see http://laravel.com/docs/html). Anyway - you need to add 'files' => true to the top of the form - so for example in the blade template it could look like:

{{ Form::open( array('route' => 'admin.store', 'class'=>'form-horizontal', 'files' => true)) }}  {{Form::file('file')}}  {{ Form::submit('Submit', array('class' => 'btn btn-info')) }}  {{ Form::close() }} 
like image 37
Andrew Avatar answered Oct 01 '22 18:10

Andrew


Looking at your code, it should work, so it looks you have a problem in anything else, maybe even your view. I just built two routers to test it here:

Route::post('file', function() {

    echo "<pre>";
    var_dump(Input::file('profile'));
    echo "</pre>";

});

Route::get('upload', function() {

    return Form::open(array('url' => '/file', 'files' => true)) .
            Form::file('profile') .
            Form::submit('upload');

});

You can use them as a baseline to, step-by-step, make yours work too.

like image 41
Antonio Carlos Ribeiro Avatar answered Oct 01 '22 20:10

Antonio Carlos Ribeiro