请建议如何使用文件系统删除文件/图像。 symfony网站上的文档不清楚,因为它提到了一些数组......
我试过了:
$fs = new Filesystem();
$fs->remove('name.jpeg');
和
的unlink(' name.jpeg&#39);
我也读过这篇文章,但没有帮助:
我也尝试过使用路径和路径+名称。但也许有一种特殊的方式来获得这条路。 谢谢
答案 0 :(得分:3)
事实证明这是你获得这条道路的方式:
$path = $this->container->getParameter('kernel.root_dir') . '/../folder1/folder2/name.jpeg';
$fs = new Filesystem();
$fs->remove($path);
答案 1 :(得分:0)
首先,您必须获取资源的绝对路径,选项为:
检索内核服务:
$kernel = $this->container->get('kernel');
对于捆绑资源:
$path = $kernel->locateResource('@NameOfBundle/Resources/public/name.jpeg');
其他资源:
// Get the absolute path of your `app` directory
$rootDir = $kernel->getRootDir();
// Retrieve the resource
$path = $rootDir . '/../web/name.jpeg';
然后,像你已经使用symfony / filesystem一样删除它:
$fs = new Filesystem();
if ($fs->exists($path)) { // This check is useless if you used $kernel->locateResource() previously
$fs->remove($path);
}
希望这对你有所帮助。