0

when the user upload new image i want to delete the previous image in folder i use Laravel....i try to make some things but not work for me any ideas?

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

#profileController
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ProfileController extends Controller
{
    public function index($slug){
        return view('profile.index')->with('data', Auth::user()->profile);
    }

    public function uploadPhoto(Request $request) {
        $file = $request->file('pic');
        $filename = $file->getClientOriginalName();
        $path = 'storage/img';

        $file->move($path, $filename);
        $user_id = Auth::user()->id;

        DB::table('users')->where('id',$user_id)->update(['pic' =>$filename]);

        return redirect('/editProfile')->withSuccess('Your image was successful.');
    }
}

3 Answers 3

1
$idname = User::where('user_id', $request->userid)->update([
    'users_profile_name' => $usersName
]);
File::delete('/public/uploads/users/".$request->userid."');

$file=$request->file('users_profile_name');
$file->move(base_path('/public/uploads/users'),$usersName);
Sign up to request clarification or add additional context in comments.

Comments

0

Use the unlink function of php,

unlink($file_path);

To create Path :

$file_path = base_path().'/images/post/'.$post->photo;

Comments

0

You can do it by using unlink() function. Here's the code from my project that deletes the previous picture if the user wants to add another:

$usersImage = public_path("/images/{$user->photo->path}"); //Finding users previous picture
        if(file_exists($usersImage)){ //If it exits, delete it from folder
            unlink($usersImage);
        }

After that you just add your logic for adding another picture

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.