在laravel上传新头像后如何删除旧头像?

时间:2019-01-26 22:52:37

标签: php laravel laravel-5

我试图在上传新头像后删除旧头像。我只是找不到解决方法。我是laravel5的新手,可以帮我吗?我正在使用来自github的项目。请在github上查看“ munafio cubetalk”。也许我需要再做些改变。当用户删除其帐户时,它也不会删除用户头像。

    public function s_general(Request $request){
    $this->validate($request,[
        'username' => 'required|min:3|max:50',
        'avatar' => 'nullable|image|mimes:jpeg,png,jpg|max:1024',
        'fullname' => 'required',
        'email' => 'required|email'
    ]);
    if ($request['username'] == Auth::user()->username &&$request['fullname'] == Auth::user()->name && $request['email'] == Auth::user()->email && !$request->hasFile('avatar')) {
        return redirect()->back()->with('general_msg', Lang::get('trans.noChanges_MSG'));
    }else{
        $avatar = $request->file('avatar');
        if ($request->hasFile('avatar')) {
            $avatar_ext = $avatar->getClientOriginalExtension();
            $avatar_name = rand(9,999999999)+time().".".$avatar_ext;
            $avatar_new = $avatar->storeAs("avatar",$avatar_name);
        }else{
            $avatar_name = Auth::user()->avatar;
        }


        $update_general = User::where('uid',Auth::user()->uid)->update(['username' => $request['username'],'name' => $request['fullname'], 'email' => $request['email'],'avatar' => $avatar_name]);
        return redirect()->back()->with('general_msg', Lang::get('trans.changes_saved'));
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用laravel delete方法来执行此操作,请确保将use Illuminate\Support\Facades\Storage;导入控制器类的顶部。

public function s_general(Request $request){
$this->validate($request,[
    'username' => 'required|min:3|max:50',
    'avatar' => 'nullable|image|mimes:jpeg,png,jpg|max:1024',
    'fullname' => 'required',
    'email' => 'required|email'
]);
if ($request['username'] == Auth::user()->username &&$request['fullname'] == Auth::user()->name && $request['email'] == Auth::user()->email && !$request->hasFile('avatar')) {
    return redirect()->back()->with('general_msg', Lang::get('trans.noChanges_MSG'));
}else{
    $avatar = $request->file('avatar');
    if ($request->hasFile('avatar')) {

        // update your code in here
        $avatar_name = Auth::user()->avatar;
        Storage::delete($avatar_name);

        $avatar_ext = $avatar->getClientOriginalExtension();
        $avatar_name = rand(9,999999999)+time().".".$avatar_ext;
        $avatar_new = $avatar->storeAs("avatar",$avatar_name);
    }else{
        $avatar_name = Auth::user()->avatar;
    }


    $update_general = User::where('uid',Auth::user()->uid)->update(['username' => $request['username'],'name' => $request['fullname'], 'email' => $request['email'],'avatar' => $avatar_name]);
    return redirect()->back()->with('general_msg', Lang::get('trans.changes_saved'));
}

}

答案 1 :(得分:0)

最后我解决了。 我发布了修改后的代码。如果以后需要帮助。

void input() {

    if (player == 'X')
    {
        cout << "\nplayer " << player << " chose your field: ";
        cin >> field;
        cout << endl;
    }
    else
    {
        pcTurn();
        cout << endl;
    }


    // instead of testing each field by its self you can just do it like this for example
    // field = 4 (filed-1)/3 is 1 & field-1 %3 is 0 so second line first column that would make it much shorter

    // now you ask the used for its choice till its an empty place
    // same thing for the pc play it will generate randoms till it finds an empty place 

    while(matrix[(field - 1) / 3][((field - 1) % 3)] != '.')
        {
            if (player == 'X')
            {

                cout << "\ninvalid choice!" << endl;
                cout << "\nplayer " << player << " chose your field: ";
                cin >> field;
                cout << endl;
            }
            else {

                pcTurn();

            }

        }

            if (player!= 'X')
            {

                cout << "\nComputer chosed field " << field << endl;
                cout << endl;
                }

            matrix[(field - 1) / 3][((field - 1) % 3)] = player;


            }

}