symfony3图像未上传

时间:2016-05-04 21:38:23

标签: image-uploading symfony

在AppBundle \ Etity \ Image我有:

    @Override
    public void onClick(View v) {
        Random r = new Random();
        int id = r.nextInt(mItemBook.numItems());
        String item = mItemBook.getItem(id);
        int pic = mItemBook.getImg(id);

        // Update text view with item
        mInfoText.setText(item);
        mImage.setImageResource(pic);

在AppBundle \ Entity \ Post.php中使用的是这样的:

Random

在AppBundle \ Form \ Type \ PostType.php中我有:

<?php

namespace AppBundle\Entity;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="images")
 * @ORM\HasLifecycleCallbacks
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank
     */
    private $name;
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\NotBlank
     */
    private $path;
    /**
     * @Assert\Image(maxSize="10M", mimeTypes="image/jpeg", minWidth = 600, minHeight = 400)
     * @Assert\NotBlank
     */
    private $file;
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank
     */
    private $alt;

    private $temp;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * @param mixed $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

    /**
     * @return mixed
     */
    public function getAlt()
    {
        return $this->alt;
    }

    /**
     * @param mixed $alt
     */
    public function setAlt($alt)
    {
        $this->alt = $alt;
    }

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

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

    public function getUploadDir()
    {
        return 'images/full';
    }

    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;

        // check if we have an old image path
        if (isset($this->path)) {
            // store the old name to delete after the update
            $this->temp = $this->path;
            $this->path = null;
        } else {
            $this->path = 'initial';
        }
    }

    /**
     * @return mixed
     */
    public function getFile()
    {
        return $this->file;


    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }

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

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->getUploadRootDir().'/'.$this->temp);
            // clear the temp image path
            $this->temp = null;
        }

        $this->file = null;
    }

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

        if ($file) {
            unlink($file);
        }
    }
}

由于某种原因,图像没有上传到指定目录(或其他任何地方),我不确定我做错了什么。任何见解我都会感激不尽。 谢谢。

1 个答案:

答案 0 :(得分:0)

问题与

有关
return __DIR__ . '/../../../../web/' . $this->getUploadDir();

在这种情况下,我将上一行更改为:

return __DIR__ . '/../../../web/' . $this->getUploadDir();

这是因为我的实体位于src / AppBundle / Entity中并且要转到根目录,它需要跳回3个目录。 在实体中对路径进行硬编码也是一个坏主意。我相应地修改了我的例子。