使用Symfony2 Sonata Admin Bundle上传Image时文件'/ tmp / phpAtfqKp'不存在

时间:2016-10-14 19:47:39

标签: php symfony doctrine-orm sonata-admin

我希望你做得很好。

任何人都可以帮我解决这个错误吗?我正在尝试使用doctrine上传图像,在图像上传后我得到文件“/ tmp / phpAtfqKp”不存在。我正在使用sonata admin bundle来创建实体。 我知道图片已上传,因为它位于 web / uploads / property 中,当进入编辑实体页面时,所有图片都显示没有任何问题。

这是我的Image实体:

<?php

namespace Hatch2Web\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Hatch2Web\MainBundle\Service\UrlService;

/**
 * Images
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="Hatch2Web\MainBundle\Repository\ImagesRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Image
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255,nullable=true)
 */
private $name;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated_at", type="datetime", nullable=true)
 */
private $updated_at;

/**
 * @ORM\ManyToOne(targetEntity="Hatch2Web\MainBundle\Entity\Property", inversedBy="images")
 */
private $property;


/**
 * @Assert\File(maxSize="50000000",
 *     mimeTypes = {"image/jpeg", "image/jpg","image/png", "image/gif", "image/x-ms-bmp"},
 *     mimeTypesMessage = "Please upload a valid Mime Type File")
 *
 */
public $file;


/**
 *
 *
 * string
 */
private $dummyUrl;


public function __construct()
{
    $this->createdAt = new \DateTime();
}


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

/**
 * Set name
 *
 * @param string $name
 * @return Image
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return Image
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set property
 *
 * @param \Hatch2Web\MainBundle\Entity\Property $property
 * @return Image
 */
public function setProperty(\Hatch2Web\MainBundle\Entity\Property $property = null)
{
    $this->property = $property;

    return $this;
}

/**
 * Get property
 *
 * @return \Hatch2Web\MainBundle\Entity\Property
 */
public function getProperty()
{
    return $this->property;
}



/** File  Upload  */


/**
 * Get file.
 *
 * @return UploadedFile
 */
public function getFile()
{
    return $this->file;
}


public function getUploadDir()
{
    return 'uploads/properties';
}


/**
 * Sets file.
 *
 * @param UploadedFile $file
 */
public function setFile(UploadedFile $file = null, $hash)
{
    if ($file) {
        $this->file = $file;
        $this->name = $hash . "." . $file->guessExtension();
    }

}


public function getDummyUrl()
{

    $object = new UrlService();
    $baseUrl = $object->getBaseUrl();
    if ($this->getName()) {
        return $baseUrl . $this->getUploadDir() . '/' . $this->getName();
    }
    return $object->getNoImage();

}

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

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

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->getFile()) {

        $filename = sha1($this->getFile()->getClientOriginalName());
        $this->name = $filename . '.' . $this->getFile()->guessExtension();

    }
}

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

    $this->file = null;
}

/**
 * Lifecycle callback to upload the file to the server
 */
public function lifecycleFileUpload()
{
    $this->upload();
}

/**
 * @ORM\PreRemove()
 *
 *
 *
 */
public function removeFile()
{
    $url = $this->getUploadDir() . '/' . $this->getName();

    try {
        if (file_exists($url)) {
            unlink($url);
        }
    } catch (\Exception $e) {
        return $e->getMessage();
    }
}

/**
 * Updates the hash value to force the preUpdate and postUpdate events to fire
 */
public function refreshUpdated()
{
    $this->setUpdatedAt(new \DateTime());
}
}

这是截图 enter image description here

0 个答案:

没有答案