2017-07-19 10 views
0

私は写真をアップロードしようとしていますが、なぜ私はいつもこのエラーメッセージが出るのか分かりません。Symfony3.2ファイルをアップロードする

Error: Call to a member function guessExtension() on a non-object

これは、取得したオブジェクトがnullであることを意味しますが、その理由は何ですか?

これは私の入力<input type="file" id="images_emp" name="images_emp" >です。私は、このやったcontrllerで今 :

$pic = $request->files->get('images_emp'); 
$imagenom=$nom.$Cin.'.'.$pic->guessExtension(); 
$pic->move($this->getParameter('Dossier_images'),imagenom); 
$Employe->setImgsrc("/images/".$imagenom); 

、これでは、企業がどのように見えるかです。

/** 
* @ORM\Column(type="string", nullable=true) 
* 
* @Assert\File(
*  maxSize = "1024k", 
*  mimeTypes={ "application/png" ,"application/jpg","application/jpeg"}, 
*  mimeTypesMessage = "Svp inserer une forme valide (png,jpg,jpeg)" 
* ) 
*/private $imgsrc; 

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

/** 
* @param mixed $imgsrc 
*/ 
public function setImgsrc($imgsrc) 
{ 
    $this->imgsrc = $imgsrc; 
    return $this; 
} 

どうすればこの問題を解決できますか。

答えて

0

私は実体でイベントを使用することができアドバイスは非常にシンプルで良いですその後、(取得)

Entity Images

<?php 
namespace ------------ ; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

/** 
* Images 
* 
* @ORM\Table(name="images") 
* @ORM\Entity(repositoryClass="---------") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Slides 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

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


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

    private $file; 

    //This attribute is added to store the temporary file name 
    private $tempFilename; 


    public function __construct() 
    { 
    } 

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


    /** 
    * Set url 
    * 
    * @param string $url 
    * 
    * @return Images 
    */ 
    public function setUrl($url) 
    { 
     $this->url = $url; 

     return $this; 
    } 

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


    public function getFile() 
    { 
     return $this->file; 
    } 


    public function setFile(UploadedFile $file = null) 
    { 
     $this->file = $file; 

     // We check if we already had a file for this entity 
     if (null !== $this->url) { 
      // The file extension is saved to be deleted later 
      $this->tempFilename = $this->url; 

      // Reset the values of the url and alt attributes 
      $this->url = null; 
      $this->alt = null; 
     } } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     // If there is no file (optional field), nothing is done 
     if (null === $this->file) { 
      return; 
     } 

     // The name of the file is its id, you just have to store its extension 
     // To make it clean, we should rename this attribute to "extension" instead of "url" 
     $this->url = $this->file->guessExtension(); 

     // And we generate the alt attribute of the <img> tag, at the file name value on the user's PC 
    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     //If there is no file (optional field), nothing is done 
     if (null === $this->file) { 
      return; 
     } 

     //If we had an old file, we delete it 
     if (null !== $this->tempFilename) { 
      $oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename; 
      if (file_exists($oldFile)) { 
       unlink($oldFile); 
      } 
     } 

     // We move the file sent in the directory of your choice 
     $this->file->move(
      $this->getUploadRootDir(), // Le répertoire de destination 
      $this->id.'.'.$this->url // Le nom du fichier à créer, ici « id.extension » 
     ); 
    } 

    /** 
    * @ORM\PreRemove() 
    */ 
    public function preRemoveUpload() 
    { 
     // We temporarily save the file name because it depends on the id 
     $this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url; 
    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() 
    { 
     // In PostRemove, we do not have access to the id, we use our saved name 
     if (file_exists($this->tempFilename)) { 
      // we delete the file 
      unlink($this->tempFilename); 
     } 
    } 

    public function getUploadDir() 
    { 
     // Returns the relative path to the image for a browser 
     return 'uploads'; 
    } 

    protected function getUploadRootDir() 
    { 
     // We return the relative path to the image for our PHP code 
     return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
    } 

    public function getWebPath() 
    { 
     return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl(); 
    } 



    /** 
    * Set description 
    * 
    * @param string $alt 
    * 
    * @return Images 
    */ 
    public function setAlt($alt) 
    { 
     $this->alt = $alt; 

     return $this; 
    } 

    /** 
    * Get alt 
    * 
    * @return string 
    */ 
    public function getAlt() 
    { 
     return $this->alt 
    } 
} 

In controller

$images = new Images(); 
$form = $this->get('form.factory')->create(ImagesType::class, $images); 
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { 
    $em = $this->getDoctrine()->getManager(); 
    $em->persist($images); 
    $em->flush(); 
} 

、あなたはopenclassroomよりさらに詳細な

でこの解決策を見つけることができます
関連する問題