0

我正在尝试使用 Symfony 4 中文件系统组件的删除功能从文件夹中删除文件。

这是我在控制器中的代码:

//Get old logo
$oldlogo = $employer->getLogo();
//If there is a old logo we need to detele it
     if($oldlogo){
       $filesystem = new Filesystem();
       $path=$this->getTargetDirectory().'/public/uploads/logos/'.$oldlogo;
       $filesystem->remove($path);
 }

private $targetDirectory;

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

public function getTargetDirectory()
    {
        return $this->targetDirectory;
    }

服务.yalm:

parameters:
    logos_directory: '%kernel.project_dir%/public/uploads/logos'

App\Controller\EmployerController:
        arguments:
            $targetDirectory: '%logos_directory%'

我没有错误消息,但文件未从文件夹中删除。

4

1 回答 1

0

I using this solution:

in my services.yaml I add:

public_directory: '%kernel.project_dir%/public'

and in my controller I use

$filesystem = new Filesystem();
$path=$this->getParameter("public_directory").'/uploads/logos/'.$oldlogo;
$filesystem->remove($path);
于 2020-05-01T19:51:39.197 回答