Laravel-尝试上传图片时出现错误:403

时间:2020-06-12 07:30:35

标签: laravel

在我的Laravel-5.8中,我要上传图片

Http \ Controllers \ HomeController

index.html

视图

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Hr\HrEmployee;
use App\Http\Requests\Hr\Employee\UploadPictureRequest;

class HomeController extends Controller
{

public function update_picture(UploadPictureRequest $request, $id)
{
    DB::beginTransaction();         
    try{ 
    $employee = HrEmployee::find($id);
             if ($request->emp_image != "") {
                 $emp_image = $request->file('emp_image');
                 $new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
                 $emp_image->move(public_path('storage/employees/image'), $new_name);
                 $employee->emp_image = $new_name;
            }        
    $employee->save();


    DB::commit();

            Session::flash('success', 'Picture Successfully Uploaded');
            return redirect()->route('dashboard');

    } 
    catch (Exception $exception) 
    {

    DB::rollback();
        Session::flash('error', 'Action failed!');
        return redirect()->back();   
    }                                                     
 }

}

视图\仪表板

该表单是称为仪表板的视图内的模式表单

路线/网络

                <span data-toggle="tooltip"  data-original-title="Click To Upload Picture">
                    <a class="btn btn-info btn-block text-white" data-toggle="modal" data-target="#upload-picture{{ $employee->id }}" data-original-title="Picture"> 
                        <b>Upload My Picture</b>
                    </a>    
                </span>

                                   <div class="modal fade" id="upload-picture{{ $employee->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form">
                                                    {{ csrf_field() }}
                                                    <div class="modal-header">
                                                        Self-Review Comment
                                                    </div>
                                                    <div class="col-md-12">
            <div class="text-center">

                            @if($employee->emp_image != '')
                                <input type="image"  src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="165" disabled/>
                                <!--<input  type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
                        <div class="row">
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                            </div>  
                           </div>   
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                                <input type="file"  name="emp_image" id="wizard-picture" class="form-control">
                            </div>  
                           </div> 
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                            </div>  
                           </div>                  
                        </div> 
                            @else
                                <input type="image" src="{{asset('theme/adminlte3/dist/img/default.png')}}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="150" disabled/>
                                <!--<input  type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
                        <div class="row">
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                            </div>  
                           </div>   
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                                <input type="file"  name="emp_image" id="wizard-picture" class="form-control">
                            </div>  
                           </div> 
                          <div class="col-12 col-sm-4">
                            <div class="form-group">
                            </div>  
                           </div>                  
                        </div>                                    
                            @endif
            </div> 
                                                    </div>

                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                                                        <button  type="submit" id="upload_pic_btn-submit" class="btn btn-success btn-ok">Save</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>     

提交表单时,出现此错误:

错误:尝试上传图片时出现403

然后,当我做PHP artisan route:list时,我得到了:

|发布| update_picture / {id} |更新图片| App \ Http \ Controllers \ HomeController @ update_picture |网络,身份验证|

我该如何解决?

谢谢。

2 个答案:

答案 0 :(得分:1)

或者您也可以尝试

 Route::put('update_picture/{id}''HomeController@update_picture')->name('update_picture');

答案 1 :(得分:0)

请在表单标签中添加此enctype =“ multipart / form-data”

<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form" enctype="multipart/form-data">

并替换此行$new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
与此行$new_name = rand() . '.' . $request->emp_image->getClientOriginalExtension();