【问题标题】:Unable to save uploaded file to filesystem with SF2无法使用 SF2 将上传的文件保存到文件系统
【发布时间】:2014-09-08 16:09:58
【问题描述】:

我正在尝试处理通过表单上传的文件并将其保存到我的文件系统。但是,当我这样做时,我总是从Symfony/Component/HttpFoundation/File/File.php 得到一个错误。

Unable to create the "/images/gallery" directory 500 Internal Server Error - FileException

在我的控制器中,我有这段代码,它处理表单验证、实体持久性等。

$gallery_image = new GalleryImage();
$form          = $this->createForm(new GalleryImageType(), $gallery_image);

// Bind the posted data to the form
$form->bind($this->getRequest());
// Make sure the form is valid before we persist the image
if ($form->isValid()) {
   // Get the entity manager and persist the contact
   $em = $this->getDoctrine()->getManager();

   $gallery_image->upload();

   $em->persist($gallery_image);
   $em->flush();

   // etc...         
}

upload 方法中我这样做:

$this->image->move('/images/gallery', 'test.'.$this->image->guessExtension());

所以我打算把它移到/web/images/gallery

我已尝试将/web 更改为777,因此问题似乎不是权限问题。

/web/images/gallery 是放置这些图像的正确位置吗?是否有任何原因导致我无法将此文件保存到文件系统?

【问题讨论】:

  • 服务器的错误日志显示什么?

标签: php image forms symfony filesystems


【解决方案1】:

您的代码是指驱动器的根目录。所以你的网络文件夹的路径不正确。

尝试更多类似的东西:

$this->image->move(__DIR__ . '/../../../../web/images/gallery', 'test.'.$this->image->guessExtension());

如果您在 web 目录中创建了图像和画廊文件夹,它应该可以工作。

参考:http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

【讨论】:

  • 为了缩短代码,您也可以使用它$this->image->move($this->get('kernel')->getRootDir().'/../web/images/gallery/', 'test.'.$this->image->guessExtension());
  • 感谢您的回复。我确实像一条无效路径一样简单。我读过食谱中的那一页,但我会专门去读那部分。
猜你喜欢
  • 2017-12-14
  • 2017-09-22
  • 1970-01-01
  • 2018-05-05
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
相关资源
最近更新 更多