2014-09-18 3 views
0

У меня есть пробник при загрузке файла. Я получаю файл, хранящийся под именем «name». но не расширение. Как с этим справиться? Heeelp пожалуйста это моя сущность:Загрузка файла Symfony 2: getClientOriginalName() вообще не работает

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

ответ

0

Вы не установили свой жизненный цикл декларации .... вот решение:

/* 
* @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

+0

Да, это работает. Спасибо – Simone

+0

@ Симон, пожалуйста, отметьте ответ как подтвержденный. –

Смежные вопросы