2015-03-03 1 views
0

Quand je charge les fichiers SonataAdminBundle ils sont chargés dans le dossier tmp J'ai cette entité:fichiers SonataAdminBundle uplaod/tmp

/** 
    * 
    * @var string 
    * 
    * @ORM\Column(type="text", length=255, nullable=false) 
    */ 
    protected $path; 

    /**  
    * @var File 
    * 
    * @Assert\File(
    *  maxSize = "5M", 
    *  mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"}, 
    *  maxSizeMessage = "The maxmimum allowed file size is 5MB.", 
    *  mimeTypesMessage = "Only the filetypes image are allowed." 
    *) 
    */ 
    protected $file; 

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

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

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

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

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

    /** 
    * 
    * @ORM\PreRemove() 
    */ 
    public function removeUpload() 
    { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 

    /** 
    * Called after entity persistence 
    * 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     if (null === $this->file) { 
      return; 
     } 

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


     $this->path = $this->file->getClientOriginalName(); 

     $this->file = null; 
    } 

Et cette forme Admin classe:

$formMapper 
    ->add('name', 'text', [ 
     'label' => 'Name' 
    ]) 
    ->add('address', 'text', [ 
     'label' => 'Address' 
    ]) 
    ->add('description', 'text', [ 
     'label' => 'Description' 
    ]) 
    ->add('file', 'file', [ 
     'label' => 'Image', 
     'data_class' => null 
    ]) 

; 

Lorsque je charge le fichier dans le panneau d'administration, puis regardez dans la base de données puis la colonne path:/tmp/php1w6Fvb

Répondre

1

Oui c'est n ormal.

Je vous recommande de lire cette partie de la documentation officielle de Symfony sur le téléchargement de fichiers.

Votre fichier est envoyé au /tmp. Si vous l'avez envoyé directement à DB sans le stocker dans un autre répertoire, il est perdu.

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

Il vous montre comment stocker ...