Symfony 2文件上载:getClientOriginalName()根本不起作用

Symfony 2文件上载:getClientOriginalName()根本不起作用,symfony,file-upload,Symfony,File Upload,我上传文件时遇到问题。我得到一个以这个名字存储的文件。但是没有扩展。 如何处理? 请帮帮忙 这是我的实体: use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\HttpFoundation\Fil

我上传文件时遇到问题。我得到一个以这个名字存储的文件。但是没有扩展。 如何处理? 请帮帮忙 这是我的实体:

 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());
}
}

您没有设置您的生命周期声明。。。。以下是解决方案:

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

英文文件