doctrine - Symfony2 Image file upload -


i trying handle image uploads via form , display these image uploads elsewhere in website.

so far have been looking @ cookbook , @ other tutorials.i have created entity , form , able submit , not sure if storing file in db or name of it.

when try display images in view resource not found error on images. suggestions on correct way upload images in symfony?

and have far. entity

<?php namespace blogbundle\entity; use symfony\component\httpfoundation\file\file; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; /** * photo * *  * @orm\entity * @orm\haslifecyclecallbacks */ class upload  {  /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id;   /**   *image file * @var file * @assert\file( maxsize = "5m",mimetypes = {"image/jpeg", "image/gif",    "image/png", "image/tiff"}, mimetypesmessage = "please upload valid image")   *    */   private $file;   /**  * @orm\column(type="string", length=500)  */ private $title; /**  * @orm\column(type="string", length=500)  */ private $description;   /**  * image path  *  * @var string  *  * @orm\column(type="text", length=255, nullable=false)  */ protected $path;    protected function getuploadrootdir() {     return __dir__.'/../../../../web/'.$this->getuploaddir(); }   protected function getuploaddir() {     // rid of __dir__ doesn't screw     // when displaying uploaded doc/image in view.     return 'uploads/documents'; }  /**  * id  *  * @return integer   */ public function getid() {     return $this->id; }  /**  * set title  *  * @param string $title  * @return upload  */ public function settitle($title) {     $this->title = $title;      return $this; }  /**  * title  *  * @return string   */ public function gettitle() {     return $this->title; }  /**  * set description  *  * @param string $description  * @return upload  */ public function setdescription($description) {     $this->description = $description;      return $this; }  /**  * description  *  * @return string   */ public function getdescription() {     return $this->description; }  /**  * set path  *  * @param string $path  * @return upload  */ public function setpath($path) {     $this->path = $path;      return $this; }  /**  * path  *  * @return string   */ public function getpath() {     return $this->path; }  /**  * called before saving entity  * @orm\prepersist()  * @orm\preupdate()  */  public function preupload()  {     if (null !== $this->file) {     // whatever want generate unique name     $filename = sha1(uniqid(mt_rand(), true));     $this->path = $filename.'.'.$this->file->guessextension(); } }  /**  * called before entity removal  *  * @orm\preremove()  */  public function removeupload()  {   if ($file = $this->getabsolutepath())    {     unlink($file);     }  }  /** * called after entity persistence * * @orm\postpersist() * @orm\postupdate() */ public function upload() { // file property can empty if field not required if (null === $this->file) {     return; }  // use original file name here should // sanitize @ least avoid security issues  // move takes target directory , // target filename move $this->file->move(     $this->getuploadrootdir(),      $this->getfile()->getclientoriginalname() );  // set path property filename you've saved file $this->path = $this->getfile()->getclientoriginalname();   // clean file property won't need anymore $this->file = null; }  /**  * sets file.  *  * @param uploadedfile $file  *@return upload  */ public function setfile(file $file = null) {     $this->file = $file;  }  /**  * file.  *  * @return uploadedfile  */ public function getfile() {     return $this->file; }     } 

controller methods

/** homepage admin area, adding,deleting,editing of blog posts.  * @route("/posted/admin/upload", name="upload")  * @template()  */  public function uploadimageaction(request $request)  {  $upload = new upload();   //create checkboxtype form $form = $this->createform(new imageformtype(), $upload, array(         'action' => $this->generateurl('upload'),         'method' => 'post',   ));  $form->handlerequest($request);  if($form->isvalid()){           $em = $this->getdoctrine()->getmanager();           $upload->upload();           $em->persist($upload);           $em->flush();   //   exit(\doctrine\common\util\debug::dump($post));        return $this->render('blogbundle:default:success.html.twig'               );             ; } return $this->render('blogbundle:default:upload.html.twig',array(           'form' =>$form->createview(),    ));  } 

you storing path in db, , it's necesary that.

using path can show file in view

pd: storing file in server with:

$this->file->move(     $this->getuploadrootdir(),     $this->getfile()->getclientoriginalname() ); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -