Laravel文件上传失败,出现419错误(会话已过期)

时间:2019-05-07 18:20:50

标签: laravel-5

我正在使用Laravel 5.7,我有一个包含一些输入字段和文件字段的表单,我在表单内有csrf字段,当我提交表单而不选择文件时,请求被接受,但是当我选择文件时,出现419错误(会话已过期)。 这是一个新问题,在我不知道会发生什么并且不再起作用之前,表单一直在工作。 我有2个版本的网站,本地版本仍然可以正常运行,但托管的版本感染了bug。 我从服务器上删除了所有文件,然后再次重新上传,但问题仍然存在。

HTML刀片:

    <form action="{{ route('account.update') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
    <div class="col-md-6 order-2 order-md-first">
        <div class="row">
            <label for="firstname">Prénom</label>
            <input @if($errors->has('firstname')) class="has-error"
                   @endif id="firstname" name="firstname" type="text"
                   value="{{ old('firstname', $user->firstname) }}"
                   placeholder="Prénom" required>

            <label for="lastname">Nom</label>
            <input @if($errors->has('lastname')) class="has-error"
                   @endif id="lastname" name="lastname" type="text"
                   value="{{ old('lastname', $user->lastname) }}"
                   placeholder="Nom"
                   required>
        </div>
    </div>
    <div class="col-md-6 text-center avatar">
        <img style="max-height:150px"
             src="{{Voyager::image($user->avatar)}}"/>
        <label class="hidden-upload" for="avatar"><i
                    class="fa fa-camera"></i><h5 class="mt-10">
                Modifier</h5></label>
        <input accept="image/jpeg, image/png, image/jpg" type="file"
               name="avatar" id="avatar" class="d-none">

    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <label>Sexe</label>
        <div class="input-radio">
            <label for="homme" class="custom-radio">
                <input
                        @if(old('gender', $user->gender)=='homme') checked
                        @endif id="homme" name="gender" value="homme"
                        type="radio"> Homme</label>
            <label for="femme" class="custom-radio">
                <input id="femme"
                       @if(old('gender', $user->gender)=='femme') checked
                       @endif name="gender"
                       value="femme"
                       type="radio">
                Femme</label>
        </div>
    </div>
    <div class="col-md-6">
        <label for="date_of_birth">Date de naissance</label>
        <input @if($errors->has('date_of_birth')) class="has-error"
               @endif id="date_of_birth" name="date_of_birth"
               value="{{ old('date_of_birth', \Carbon\Carbon::parse($user->date_of_birth)->format('d/m/Y')) }}"
               autocomplete="off"
               placeholder="JJ/MM/AAAA (E.g.: 12/05/1970)" type="text">
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <label for="email">E-mail</label>
        <input @if($errors->has('email')) class="has-error"
               @endif id="email"
               type="email" name="email"
               value="{{ old('email', $user->email) }}"
               placeholder="E-mail" required></div>
    <div class="col-md-6">
        <label for="phone">Numéro de téléphone</label>
        <input @if($errors->has('phone')) class="has-error"
               @endif id="phone"
               type="phone" name="phone"
               value="{{ old('phone', $user->phone) }}"
               placeholder="Numéro de téléphone" required></div>
</div>
<div class="row">

    <div class="col-md-6">

        <label for="password">Mot de passe</label>
        <input @if($errors->has('password')) class="has-error"
               @endif id="password" type="password" name="password"
               placeholder="Mot de passe">
        <span class="example">Laissez le mot de passe vide pour conserver le mot de passe actuel.</span>
    </div>
    <div class="col-md-6"><label for="password-confirm">Confirmer le mot
            de
            passe</label>
        <input @if($errors->has('password_confirmation')) class="has-error"
               @endif id="password-confirm" type="password"
               name="password_confirmation"
               placeholder="Confirmer le mot de passe">
    </div>
</div>
<div class="row">
    <div class="col-12">
        <div class="button-box">
            <button type="submit" class="default-btn">Enregistrer
            </button>
        </div>
    </div>
</div>
</form>

控制器:

public function update(Request $request)
{
    $request->validate([
        'firstname' => 'required|string|max:255',
        'lastname' => 'required|string|max:255',
        'phone' => 'required|numeric|min:8',
        'date_of_birth' => 'required|date_format:d/m/Y|before:today',
        'gender' => 'required|string|in:homme,femme',
        'email' => 'required|string|email|max:255|unique:users,email,' . auth()->id(),
        'password' => 'sometimes|nullable|string|min:6|confirmed',
    ]);
    $user = auth()->user();
    $input = $request->except('password', 'password_confirmation');

    if ($request->hasFile('avatar')) {
        $count = 1;
        if ($user->avatar != "users/default.png")
            Storage::delete("public/" . $user->avatar);
        $user->avatar = str_replace("public/", "", Storage::putFile('public/users', $request->avatar), $count);
    }

    if (!$request->filled('password')) {
        $user->fill($input);
        $user->date_of_birth = date("Y-m-d", strtotime($request->date_of_birth));
        $user->save();

        return back()->with('success_message', 'Vos informations ont été mise a jours!');
    }

    $user->password = bcrypt($request->password);
    $user->fill($input);
    $user->date_of_birth = date("Y-m-d", strtotime($request->date_of_birth));
    $user->save();

    return back()->with('success_message', 'Vos informations (et mot de passe) ont été mise a jours!');
}

1 个答案:

答案 0 :(得分:-2)

修复此问题的最佳方法是使用版本更新laravel