Laravel 5.2无法访问上传的文件

时间:2016-01-14 03:37:10

标签: php laravel-5

我已经有一段时间了。希望我错过了一些简单的东西,但我无法让Laravel看到我通过表单上传的文件。

我尝试上传为有人注册,但此时,我甚至无法在这个简单的测试控制器中识别该文件。

查看

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Register</div>
            <div class="panel-body">
                <form class="form-horizontal" role="form" method="POST" action="{{ url('/test') }}" enctype="multipart/form-data">
                    {!! csrf_field() !!}

                    <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">First Name</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" maxlength="50">

                            @if ($errors->has('first_name'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('first_name') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Last Name</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" maxlength="50">

                            @if ($errors->has('last_name'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('last_name') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-6">
                            <input type="email" class="form-control" name="email" value="{{ old('email') }}" maxlength="255">

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input type="password" class="form-control" name="password" maxlength="60">

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Confirm Password</label>

                        <div class="col-md-6">
                            <input type="password" class="form-control" name="password_confirmation" maxlength="60">

                            @if ($errors->has('password_confirmation'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password_confirmation') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('type') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Type</label>

                        <div class="col-md-6">
                            <select class="form-control" name="type">
                                <option {{ (old("type") == null ? "selected":"") }} disabled value="">--How will you use this site?--</option>
                                <option value="consultant" {{ (old("type") == 'consultant' ? "selected":"") }}>Consultant (Find Leads)</option>
                                <option value="sales" {{ (old("type") == 'sales' ? "selected":"") }}>Sales (Sell Leads)</option>
                                <option value="both" {{ (old("type") == 'both' ? "selected":"") }}>Both</option>
                            </select>


                            @if ($errors->has('type'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('type') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>


                    <div class="form-group{{ $errors->has('about') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">About</label>

                        <div class="col-md-6">
                            <textarea class="form-control" name="about" rows="5" maxlength="500" placeholder="Why should businesses accept your leads?"></textarea>


                            @if ($errors->has('about'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('about') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 control-label" for="file">Attach Profile Picture</label>
                        <div class="col-md-6">
                            <input class="form-control" type="file" name="photo" id="photo">
                            <p class="help-block">Max Size :500KB</p>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-btn fa-user"></i>Register
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</div>
@endsection

控制器

<?php namespace App\Http\Controllers;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Illuminate\Http\Request;

class TestController extends Controller {
    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        if ($request->hasFile('photo')) {
            return 'has file';
        } else {
            return 'no file';
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我的代码很好。只需要添加php.ini文件并包含下面的大小限制(10 MB):

<强>的php.ini:

upload_max_filesize = 10M 
post_max_size = 10M