【问题标题】:Symfony forms. File uploadSymfony 形式。上传文件
【发布时间】:2012-07-27 02:51:25
【问题描述】:

尝试使用 Entity 管理文件上传,但出现此错误:

致命错误:在第 327 行调用堆栈:0.0002 333264 1. {main}( ) /home/projectpath/web/app_dev.php:0 0.0450 1158160...

这是实体类:

namespace BS\BackstretcherBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
* MB\MyBundle\Entity\Items
*
* @ORM\Table(name="items")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Items
{
    private $filenameForRemove;

    /**
     * @Assert\File(maxSize="60000000")
     */
    public $file;
    ...

    protected function getUploadDir()
    {
       return 'images/items/';
    }

    protected function getUploadRootDir()
    {
      return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    public function getWebPath()
    {
      return null === $this->file ? null : $this->getUploadDir().'/'.$this->getNameEn();
    }

    public function getAbsolutePath()
    {
      return null === $this->file ? null : $this->getUploadRootDir().'/'.$this->getNameEn().'.jpg';
    }

  /**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
    public function preUpload()
    {
      if (null !== $this->file)
      {
         $this->file = $this->getId() .'.'. $this->file->guessExtension();
      }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
      if (null === $this->file)
      {
        return;
      }

      $this->file->move($this->getUploadRootDir(), $this->file);
      unset($this->file);
    }

    /**
     * @ORM\PostRemove()
     */
     public function removeUpload()
     {
       if ($file = $this->getAbsolutePath())
       {
         unlink($file);
       }
     }

还有控制器:

public function new_productAction(Request $request)
{
  $product = new Items();
  $product->setPrice(0);

  $form = $this->createFormBuilder($product)
    ->add('Type', 'choice', array(
      'choices'   => array('1' => 'Product', '0' => 'Article'),
      'required'  => false,))
    ->add('Price',  'number')
    ->add('nameEn', 'text')
    ->add('file', 'file', array('label' => 'Image', 'required' => true))
    ->getForm();

  if ($request->getMethod() == 'POST')
  {
    if ($form->isValid())
    {
      $form->bindRequest($request);
      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($product);
      $em->flush();

      return new Response('<html><body>Success!</body></html>');
    }
  }

  return $this->render('MyBundle:Default:admin_page.html.twig', array(
    'form' => $form->createView(),
  ));
}

Symfony 版本:2.1.0

【问题讨论】:

    标签: forms upload entity symfony-2.1


    【解决方案1】:

    检查您的 php.ini 文件并确保 post_max_size 和 upload_max_filesize 都设置得足够大。

    【讨论】:

      【解决方案2】:

      我认为 duke_nukem 已经不再担心这个问题了,6 个月后,但如果其他人遇到这个问题,我会遇到完全相同的问题并在这里得到了很好的答案:

      Error with file upload in symfony 2

      看起来 duke_nukem 和我犯了同样的错误。 preUpload() 方法应为:

        /**
         * @ORM\PrePersist()
         * @ORM\PreUpdate()
         */
          public function preUpload()
          {
            if (null !== $this->file)
            {
               $this->path = $this->getId() .'.'. $this->file->guessExtension();
            }
          }
      

      当前代码将$this-&gt;file 转换为字符串,导致错误。路径实际上应该分配给$this-&gt;path

      另一个问题中的 Sybio 发现了这一点,而不是我。我只是想传播爱。

      【讨论】:

        【解决方案3】:

        很奇怪

        您的控制器中的代码有误。您必须在验证之前将您的请求绑定到您的表单。之后,您可以检索您的数据

        if ($request->getMethod() == 'POST')
          {
            //Note: bindRequest is now deprecated
            $form->bind($request);
            if ($form->isValid())
            {
              //retrieve your model hydrated with your form values
              $product = $form->getData();
        
              //has upload file ?
              if($product->getFile() instanceof UploadedFile){
                //you can do your upload logic here wihtout doctrine event if you want
              }
        
              $em = $this->getDoctrine()->getEntityManager();
              $em->persist($product);
              $em->flush();
        
              return new Response('<html><body>Success!</body></html>');
            }
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多