【问题标题】:Symfony 2 file upload: getClientOriginalName() doesn't work at allSymfony 2 文件上传:getClientOriginalName() 根本不起作用
【发布时间】:2014-09-18 13:43:26
【问题描述】:

我在上传文件时遇到了问题。我得到一个存储在名称“名称”下的文件。但没有扩展。 如何处理? 请帮帮我 这是我的实体:

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


 /**
 * fichier
 *
 * @ORM\Table()
 * @ORM\Entity
 */
  class fichier
 {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 * @ORM\ManyToOne(targetEntity="rex\Bundle\Entity\fiche")
 * @ORM\JoinColumn
 */
private $titreFiche;

/**
 * @param string $titreFiche
 */
public function setTitreFiche($titreFiche)
{
    $this->titreFiche = $titreFiche;
}

/**
 * @return string
 */
public function getTitreFiche()
{
    return $this->titreFiche;
}

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

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 */
public $name;

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

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

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
public $path;

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

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

/**
 * @Assert\File
 *     maxSize = "50000000",
 *     maxSizeMessage = "Votre fichier est trop gros ({{ size }}). La taille maximum autorisée est : {{ limit }}")
 *
 */
public $file;

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

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

// Upload d'image

public function getFullImagePath()
{
    return null === $this->name ? null : $this->getUploadRootDir().$this->name;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return $this->getTmpUploadRootDir().$this->getId()."/";
}

protected function getTmpUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/uploads/';
}


/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUploadImage()
{

        if (null !== $this->file) {
           // $name = sha1(uniqid(mt_rand(), true));
            $this->name = 'name.'.$this->file->getClientOriginalName();

        }
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function uploadImage()
{

    if (null === $this->file) {
        return;
    }

    if(!is_dir($this->getUploadRootDir())){
        mkdir($this->getUploadRootDir());
    }

    $this->file->move($this->getUploadRootDir(), $this->name);

    unset($this->file);
}



/**
 * @ORM\PostRemove()
 */
public function removeImage()
{
    unlink($this->getFullImagePath());
    rmdir($this->getUploadRootDir());
}
}

【问题讨论】:

    标签: symfony file-upload


    【解决方案1】:

    你没有设置你的生命周期声明......这是解决方案:

     /*
     * @ORM\Table()
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
     class fichier
    

    法语文档

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

    英文文档

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2011-03-07
      • 2012-11-13
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      相关资源
      最近更新 更多