mardi 10 septembre 2019

Delete previous image from folder when updated by new image

How can i delete the previous image from folder when it is update by new image.

in the code that I made it only added a new image in the image folder, and the previous image was not deleted.

this is my Controller

public function update(Request $request, $id)
{
    $image_name = $request->hidden_image;
    $image      = $request->file('image');
    if ($image != '')
     {
      $request->validate(['judul' => 'required',
        'image' => 'image|max:2048'
    ]);
    $image_name = rand() . '.' . $image->getClientOriginalExtension();
    $image->move(public_path('uploadberita'), $image_name); 
     }
     else{
        $request->validate([
            'judul' => 'required',
            'image' => 'required',
            'artikel' => 'required'
        ]);
     }
     $form_data  = array(
            'judul' => $request->judul,
            'image' => $image_name,
            'artikel' => $request->artikel 
        );

     Berita::whereId($id)->update($form_data);
     return redirect()->route('admin.berita.index')->with('pesan', 'Data is Successfully update');
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/308c59s
via IFTTT

Aucun commentaire:

Enregistrer un commentaire