2017-08-09 48 views
0

我的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文件不變,有人可以解釋我做錯了什麼這裏??

回答

1

的原因,你的if語句不工作是因爲'woman.png'將評估到true所以你的if語句基本上是:

if ($user->avatar !== 'man.png' || true) 

這將永遠是true

更改您的if語句是:

if ($user->avatar !== 'man.png' && $user->avatar !== 'woman.png') 

    $file = public_path('uploads/avatars/' . $user->avatar); 

    if (File::exists($file)) { 
     unlink($file); 
    } 
} 

希望這有助於!

+0

似乎是一個好的解決方案,我已經實現了它,工作,但現在它只是保持默認圖像(一E不上傳新圖像)... – AtomisedClarity

+0

@AtomisedClarity你是否仍然有保存新​​圖像的原始邏輯,或者你刪除了它? –

+0

是的,我刪除了其餘的.... – AtomisedClarity

0

好吧,

我得到它像

$user = User::find(Auth::user()->id); 

// Handle the user upload of avatar 
if ($r->hasFile('avatar')) { 
    $avatar = $r->file('avatar'); 
    $filename = time() . '.' . $avatar->getClientOriginalExtension(); 

} 



    // Delete current image before uploading new image 

if ($user->avatar !== 'man.png' && $user->avatar !== 'woman.png') 

     $file = 'uploads/avatars/' . $user->avatar; 

if (File::exists($file)) { 
    unlink($file); 
} 

    Image::make($avatar)->resize(300, 300)->save('uploads/avatars/' . 
    $filename); 
    $user = Auth::user(); 
    $user->avatar = $filename; 
    $user->save();   
    return back()->with('msg', 'Profiel is bijgewerkt'); 



       } 




    }