php Input::file() 返回 null Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22513355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:58:24  来源:igfitidea点击:

Input::file() returning null Laravel

phplaravellaravel-4

提问by noahdotgansallo

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.

我一直在编写一个上传脚本,即使我使用的是 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);
        }
    }

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?

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

Here is my HTML form stuff:

这是我的 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

回答by DenLanden

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

我在正常形式下遇到了同样的问题。我忘了补充:

enctype="multipart/form-data"

回答by Andrew

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' => trueto the top of the form - so for example in the blade template it could look like:

我遇到了同样的问题,并意识到我在表格中遗漏了一些东西。使用内置的 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() }}

回答by Antonio Carlos Ribeiro

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.

您可以将它们用作基线,逐步使您的工作也能正常工作。