When I'm trying to update image it's works properly but old image doesn't delete.

Posted 8 months ago by shamsul_huda

Here is my code:

$post = Post::find($id);
        $filename  = public_path('uploads/posts/').$post->featured;

        if ($request->hasFile('featured'))
        {
            
            $featured = $request->featured;

            $featured_new_name = time() . $featured->getClientOriginalName();

            $featured->move('uploads/posts', $featured_new_name);

            // $oldFileName = $post->featured;
            // give new name of featurerd_new_name

            $post->featured = 'uploads/posts/'.$featured_new_name;

            // delete old one
            File::delete($filename);
        }

Please sign in or create an account to participate in this conversation.