我的Laravel(5.4)網站上有一個上傳頭像部分,如果用戶還沒有上傳頭像,取決於他們的性別,它會顯示一個男性或女性「默認」的頭像,不要在上傳新頭像後刪除「默認」頭像圖片
,所有工作正常,並上傳一個新的工作正常,但上傳新的頭像會刪除或覆蓋默認頭像時
(我UserController中)
public function update(Request $r)
{
$this->validate($r, [
'location' => 'required',
'about' => 'required|max:355'
]);
Auth::user()->profile()->update([
'location' => $r->location,
'about' => $r->about
]);
return back()->with('msg', 'Profile successfully edited');
$user = User::find(Auth::user()->id);
// Handle the user upload of avatar
if ($r->hasFile('avatar')) {
$avatar = $r->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save('uploads/avatars/' . $filename);
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
// Delete current image before uploading new image
if ($user->avatar !== 'man.png' || 'woman.png') {
//$file = public_path('uploads/avatars/' . $user->avatar);
$file = 'uploads/avatars/' . $user->avatar;
//$destinationPath = 'uploads/' . $id . '/';
}
if (File::exists($file)) {
unlink($file);
}
return back()->with('msg', 'Profile successfully edited');
}
}
在我的RegisterController我有
protected function create(array $data)
{
if($data['gender']){
$avatar = 'man.png';
}
else{
$avatar = 'woman.png';
}
在我的(配置文件)edit.blade.php
<form enctype="multipart/form-data" action="{{ route('profile.update') }}"
method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="avatar" >Change Avatar</label>
<input type="file" name="avatar" class="form-control" accept="image/*">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
我想繼續要留給男性和female.png文件不變,有人可以解釋我做錯了什麼這裏??
似乎是一個好的解決方案,我已經實現了它,工作,但現在它只是保持默認圖像(一E不上傳新圖像)... – AtomisedClarity
@AtomisedClarity你是否仍然有保存新圖像的原始邏輯,或者你刪除了它? –
是的,我刪除了其餘的.... – AtomisedClarity