Symfony Delete image uploaded with filesystem

i would like to delete an image from my / uploads folder with fileSystem but I can’t do it. Here is my code.
Thank you so much !

/**
 * Permet de supprimer des images
 *
 * @Route("/delete/image/{id}", name="image_delete", methods={"DELETE"})
 *
 * @param Image $image
 * @param EntityManagerInterface $manager
 * @return Response
 */
public function deleteImage(Image $image, EntityManagerInterface $manager)
{
    /**
     * Je gère la suppression du dossier "uploads" ou l'image est stockée
     */
    //Je récupère le nom de l'image
    $filename = $image->getName();
    // Je crée une instance de kla classe fileSystem
    $fileSystem = new Filesystem();
    //Je supprime l'image du dossier
    $fileSystem->remove('%kernel.project_dir%/public/uploads/'.$filename);

    /**
     * Je gère la suppression en bdd
     */
    $manager->remove($image);
    $manager->flush();
    return new Response('deleted', Response::HTTP_OK);



}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *