gpt4 book ai didi

symfony - 从Symfony 3中的文件夹中删除文件

转载 作者:行者123 更新时间:2023-12-03 15:30:02 27 4
gpt4 key购买 nike

我有一个带有createActioneditActiondeleteAction的 Controller 。

我可以添加图片并将其从数据库中删除。

当我遵循Symfony文档创建上传器文件时,我也在config.yml中创建了此路由

parameters:
uploaded_restaurants: "%kernel.root_dir%/../web/uploads/restaurants"

这就是我上传的文件的路径。

我的问题

当我使用删除操作删除图片时,可以将其删除到我的数据库中。但不在我的文件夹 /web/uploads/restaurants
我试图找到一种方法也可以将其删除,但无法成功。

我也尝试过用 Filesystem()删除它们,但是我一定写得不好。

这是我的 Controller :
    class MediaController extends Controller
{
/**
* @param Request $request
* @param Restaurant $restaurant
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function newAction(Request $request, Restaurant $restaurant)
{
$media = new Media();
$form = $this->createForm(MediaType\CreateType::class, $media);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$mediaList = $this->getDoctrine()->getRepository('AppBundle:Media')->findImageByRestaurantType($restaurant, $form->get('context')->getData());

if ($mediaList == null || count($mediaList) < 1) {
$media->setType('img')
->setContext($form->get('context')->getData())
;
} else {
$media = $mediaList;
}

$imageFile = $request->files->get('create')['name'];
$targetDir = $this->getParameter('uploaded_restaurants');
$imageName = $this->get('app.uploader')->upload($imageFile, $targetDir);

/** @var $originalName */
$originalName = $imageFile->getClientOriginalName();
$media->setName($imageName)
->setOriginalName($originalName)
->setCreatedAt(new \DateTime());

$em = $this->getDoctrine()->getManager();
$media->setRestaurant($restaurant);
$em->persist($media);
$em->flush();

return $this->redirectToRoute('admin_restaurant_show_fr', array('id' => $restaurant->getId()));
}
return $this->render('admin/restaurant/media/new.html.twig', array(
'restaurant' => $restaurant,
'form' => $form->createView()
));
}

/**
* @param Request $request
* @param Media $media
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, Media $media)
{
$em = $this->getDoctrine()->getManager();

$editForm = $this->createForm(MediaType\EditType::class, $media);
$editForm->handleRequest($request);
$restaurant = $media->getRestaurant();

if ($editForm->isSubmitted() && $editForm->isValid()) {
$imageFile = $request->files->get('edit')['name'];
$targetDir = $this->getParameter('uploaded_restaurants');
$imageName = $this->get('app.uploader')->upload($imageFile, $targetDir);

if ($request->files->get('edit')['name'] != null) {

$fs = new Filesystem();
try {
$fs->remove($this->get('kernel')->getRootDir() . '/../web/uploads/restaurants' . $media->getName());
} catch (IOException $e) {
echo "error";
}

/** @var $originalName */
$originalName = $imageFile->getClientOriginalName();
$media->setName($imageName)
->setOriginalName($originalName)
->setUpdatedAt(new \DateTime());
}
$em->persist($media);
$em->flush();

return $this->redirectToRoute('admin_restaurant_show_fr', array('id' => $restaurant->getId()));
}
return $this->render('admin/restaurant/media/edit.html.twig', array(
'media' => $media,
'restaurant' => $restaurant,
'editForm' => $editForm->createView()
));
}

/**
* @param Media $media
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function deleteAction(Media $media)
{
$restaurant = $media->getRestaurant();
$em = $this->getDoctrine()->getManager();
$em->remove($media);
$em->flush();

return $this->redirectToRoute('admin_restaurant_show_fr', array('id' => $restaurant->getId()));
}
}

如您所见,在editAction中,我尝试添加 Filesystem以删除文件

我的问题

如何为 editActiondeleteAction执行此操作,以便从目录中正确删除文件?
如果您不太了解我的代码,是否有示例可以向我展示其效果?
谢谢

最佳答案

在编辑或删除媒体时,您都可以使用现有对象及其文件名,例如:

public function deleteAction(Media $media){ ... }

Doctrine将从URL中读取ID,并从数据库中加载相应的媒体。您可以使用该对象获取旧图像的位置,然后删除该图像。
// getName() will contain the complete filename with path.
// Check your showAction where you call `setName($imageName)`
$filename = $media->getName();

$filesystem = new Filesystem();
$filesystem->remove($filename);

您可以在editAction而不是您提供的代码中执行相同的操作。

关于symfony - 从Symfony 3中的文件夹中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42906587/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com