2

I have a controller with a supprimer action.

I can add files and delete them from the database.

As I followed the Symfony doc to create an uploader file, I also created in my services.yml this route

parameters:
 repertoire_soumissions: '%kernel.project_dir%/public/uploads/LettresSoumissios'

My problem

When I delete my file with my supprimer action, it's fine it deletes it in my database. But not in my folder /public/uploads/LettresSoumissions. I tried to find a way to be able to delete them in my folder too, but couldn't succeed it.

I tried also with a Filesystem() to remove them, but I've must have written it badly.

Here is my action in my controller class

/**
     * @Route("admin/soumission/{id}/supprimer", name="supprimer_soumission")
     */
    public function supprimerSoumission(Soumission $soumission, ObjectManager $manager){

        $lettresoumission= $soumission->getLettreSoumission();

        $filesystem = new Filesystem();

        $path='%kernel.project_dir%/public/uploads/LettresSoumissios/'.$lettresoumission;
        $filesystem->remove($path);
        $manager->remove($soumission);
        $manager->flush();
        $this->addFlash('success','Soumission supprimer !!');

        return $this->redirectToRoute('soumission');
    }
Yaman Jain
  • 1,254
  • 11
  • 16
m.deye
  • 41
  • 2
  • 6

2 Answers2

2

Could it be due to a mispelling in the path name?

$path='%kernel.project_dir%/public/uploads/LettresSoumissioNs/'.$lettresoumission;

Missing a N letter.

Veve
  • 6,643
  • 5
  • 39
  • 58
Kevin PESENTI
  • 46
  • 1
  • 4
  • '%kernel.project_dir%/public/uploads/LettresSoumissios/' is the path name, in my uploads folder i have LettresSoumissios folder – m.deye Apr 29 '19 at 15:00
2

Your problem is this line:

$path='%kernel.project_dir%/public/uploads/LettresSoumissios/'.$lettresoumission;

The parameter above is simply a string with value:

%kernel.project_dir%

You need a way to retrive the value of the parameter.

A clean way would be to inject the parameter value in the controller.

Define your Controller as a service in your service.yaml:

Namespace\NameOfTheController:
        arguments:
            - '%kernel.project_dir%'
        tags: [controller.service_arguments]

Inject the value in the Constructor of your Controller:

private $kernelRoot;

public function __construct(string $kernelRoot)
{
   $this->kernelRoot = $kernelRoot;
}

Now change your code-line to:

$path=$this->kernelRoot.'/public/uploads/LettresSoumissios/'.$lettresoumission;

to clarify:

You said:

I also created in my services.yml this route

parameters:
 repertoire_soumissions: '%kernel.project_dir%/public/uploads/LettresSoumissios'

This is no service or route declaration, this is just a parameter => value mapping.

Thomas Baier
  • 434
  • 3
  • 8
  • I'm glad I was able to help! – Thomas Baier Apr 29 '19 at 15:20
  • repertoire_soumissions: '%kernel.project_dir%/public/uploads/LettresSoumissios' this instruction delete again the folder i dont want to remove the folde juste the file, how can i resolve this problem – m.deye Apr 30 '19 at 11:00
  • Your path was `$path=$this->kernelRoot.'/public/uploads/LettresSoumissios/'.$lettresoumission;` so your filename is in the variable `$lettresoumission` delete path should be `$path=$this->kernelRoot.'/public/uploads/LettresSoumissios/'.$lettresoumission;` if not check your code ;) – Thomas Baier Apr 30 '19 at 11:05
  • you'r tottaly right brother, i hade a small error. Thank you again – m.deye Apr 30 '19 at 11:40
  • Now you got it ;) – Thomas Baier Apr 30 '19 at 11:52