我按照文档将文件上传到我的公用文件夹,现在我希望可以从该文件夹中删除一个文件。
如果我使用api平台提供的查询:api/image/{id},我只能删除表中的行。我必须检索图像的路径才能将其保存在表中。也许我可以用它来删除图像?
我需要一个沙漠化器?
劳伦特。
您需要创建一个EventListener
并侦听postRemove
事件,以便在从Image
实体中删除条目后,从目录中物理删除该文件。
在图像实体上侦听事件的示例:
class ImageEventSubscriber
{
public function postRemove(Image $image, LifecycleEventArgs $event){
$image_path = $image->getPath(); // You may have to specify the full path to the file if it is not listed in the database.
if (file_exists($image_path))
{
unlink($image_path);
}
}
}
现在您需要在services.yaml文件中声明您的事件侦听器
AppEventListenerImageEventSubscriber:
tags: [ { name: doctrine.orm.entity_listener, entity: AppEntityImage, event: postRemove } ]
现在,当请求DELETE: api/image/{id}
时,记录和文件将被删除。您可以通过阅读文档获得有关EventSubscriber的更多信息。