Input :: file()返回null Laravel

noa*_*llo 25 php laravel laravel-4

我一直在编写一个上传脚本,即使我使用的是Laravel内置函数Input :: file(),它仍然会返回null.我要发布我的家庭控制器代码.

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我死(var_dump(Input :: file('profile')))时返回null,这意味着它基本上不会将我的文件作为实际文件.当我死的时候(var_dump(Input :: get('profile')))它会返回图像名称,这意味着我的上传脚本正在运行但是Laravel后端失败了.这是真的吗,还是我错过了什么?

这是我的HTML表单:

@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
Run Code Online (Sandbox Code Playgroud)

小智 78

我在正常形式下遇到了同样的问题.我忘了添加:

enctype="multipart/form-data"
Run Code Online (Sandbox Code Playgroud)


小智 27

我有同样的问题,并意识到我在表格中遗漏了一些东西.使用内置的laravel表单,因为它会自动添加CRSF保护(请参阅http://laravel.com/docs/html).无论如何 - 你需要添加'files' => true到表单的顶部 - 例如在刀片模板中它可能看起来像:

{{ Form::open( array('route' => 'admin.store', 'class'=>'form-horizontal', 'files' => true)) }}

{{Form::file('file')}}

{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}

{{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)