在Laravel注册中上传图片

时间:2017-12-17 17:25:19

标签: php image laravel upload registration

我正在创建一个网站,用户可以使用Laravel身份验证上传自己的个人资料图片,其中包含姓名,电子邮件,密码和信息图片等凭据。

执行php artisan make:auth后,AuthController.php中有此功能:

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'dob' => $data['dob'],
        'profile_picture' => $data['profile_picture],
    ]);
}

这是我的表格:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}" enctype="multipart/form-data">
    {{ csrf_field() }}

    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
        <label for="email" class="col-md-4 control-label">E-mail Address</label>
        <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">

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

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

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

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

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

    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
        <label for="name" class="col-md-4 control-label">Fullname</label>
        <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}">

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

    <div class="form-group{{ $errors->has('dob') ? ' has-error' : '' }}">
        <label for="name" class="col-md-4 control-label">Date of Birth</label>
        <input id="dateOfBirth" type="date" class="form-control" name="dob">

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

     <div class="form-group{{ $errors->has('profile_picture') ? ' has-error' : '' }}">
        <label for="name" class="col-md-4 control-label">Profile Picture</label>
        <input id="profilePicture" type="file" class="form-control" name="profile_picture">

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

    <div class="form-group">
        <button type="submit" class="btn btn-primary">
            <i class="fa fa-btn fa-user"></i> Register
        </button>
    </div>
</form>

我想用扩展名保存图片文件名。但是我该怎么办呢? 如何只在字符串中获取文件名,将图像文件移动到公共文件夹,并将其保存到XAMPP数据库中?

2 个答案:

答案 0 :(得分:4)

如果您只想保存名称并上传到profile_images文件夹中的公共目录,那么您可以在protected function create

中执行此类操作
            $request = request();

            $profileImage = $request->file('profile_picture');
            $profileImageSaveAsName = time() . Auth::id() . "-profile." . 
                                      $profileImage->getClientOriginalExtension();

            $upload_path = 'profile_images/';
            $profile_image_url = $upload_path . $profileImageSaveAsName;
            $success = $profileImage->move($upload_path, $profileImageSaveAsName);


  return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'dob' => $data['dob'],
        'profile_picture' => $profile_image_url,
    ]);

答案 1 :(得分:0)

通过获取扩展名和设置文件名,您可以将文件移动到公用文件夹中并保存数据
这是创建功能保存文件

protected function create(array $data)
{
    $file_extention = $data['img_profile']->getClientOriginalExtension();
    $file_name = time().rand(99,999).'image_profile.'.$file_extention;
    $file_path = $data['img_profile']->move(public_path().'/users/image',$file_name);

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'dob' => $data['dob'],
        'profile_picture' => $file_path, // save full image path to database
    ]);
}

希望对您有帮助