php - Issue with Doctrine/Symdony2.2.1 uploading files -


i've been working symfony project , i've been experiencing issues uploading files, wondered if point me in right direction?

here's controller:

<?php  namespace file\irbundle\controller;  use symfony\component\httpfoundation\request; use symfony\bundle\frameworkbundle\controller\controller; use file\irbundle\entity\file; use sensio\bundle\frameworkextrabundle\configuration\template;  class defaultcontroller extends controller {     public function indexaction()     {         return $this->render('fileirbundle:default:index.html.twig', array('name' => $name));     }  public function upload() {     if (null === $this->getfile()) {         return;     }      $this->getfile()->move(         $this->getuploadrootdir(),         $this->getfile()->getclientoriginalname()     );      $this->path = $this->getfile()->getclientoriginalname();     $this->file = null; }  /**  * @template()  */ public function uploadaction(request $request) {     $file = new file();      $form = $this->createformbuilder($file)                  ->add('title', null, array(                     'label' => 'title'                  ))                  ->add('impulseresponsetype', 'choice',  array(                     'choices' => array(                         'environment' => 'environment',                         'hardware'    => 'hardware',                         'speaker'     => 'speaker'                     ),                     'label' => 'impulse response type'                  ))                  ->add('impulsesource', 'choice', array(                     'choices' => array(                         'sine sweep'        => 'sine sweep',                         'balloon pop'       => 'balloon pop',                         'white-noise burst' => 'white-noise burst',                         'start pistol'      => 'start pistol',                         'hand clap'         => 'hand clap',                         'other'             => 'other'                     ),                     'label' => 'impulse source'                  ))                  ->add('softwareused', null, array(                     'label' => 'software used'                  ))                  ->add('bitdepth', 'choice', array(                     'choices' => array(                         '16' => '16-bit',                         '24' => '24-bit',                         '32' => '32-bit float'                     ),                     'label' => 'bit-depth'                  ))                  ->add('samplerate', 'choice', array(                     'choices' => array(                         '44100'  => '44.1khz',                         '48000'  => '48khz',                         '88200'  => '88.2khz',                         '96000'  => '96khz',                         '192000' => '192khz'                     ),                     'label' => 'sample rate'                  ))                  ->add('impulseformat', 'choice', array(                     'choices' => array(                         'mono'   => 'mono',                         'stereo' => 'stereo'                     ),                     'label' => 'impulse format'                  ))                  ->add('microphoneused', null, array(                     'label' => 'microphone used'                  ))                  ->add('preampused', null, array(                     'label' => 'pre-amp used'                  ))                  ->add('location', null, array(                     'label' => 'location'                  ))                  ->add('description', null, array(                     'label' => 'description'                  ))                  ->add('tags', null, array(                     'label' => 'tags (separate comma\'s)'                  ))                  ->add('file', null, array(                     'label' => 'select file'                  ))                  ->getform();         if ($this->getrequest()->ismethod('post')) {             $form->bind($this->getrequest());             if ($form->isvalid()) {             $em = $this->getdoctrine()->getmanager();              $user = $this->get('security.context')->gettoken()->getuser();             $id = $user->getid();              $file->setdate(new \datetime());             $file->setuser_id($user);              $em->persist($file);             $em->flush();              return $this->redirect($this->generateurl('site_index'));             }         }      return array('form' => $form->createview()); } } 

here's entity:

<?php  namespace file\irbundle\entity;  use doctrine\orm\mapping orm; use symfony\component\httpfoundation\file\uploadedfile; use symfony\component\validator\constraints assert;  /**  * @orm\entity  * @orm\haslifecyclecallbacks  */ class file {     /**      * @orm\id      * @orm\column(type="integer")      * @orm\generatedvalue(strategy="auto")      */     public $id; /**  * @param $temp  */ private $temp;  /**  * @assert\file(maxsize="1048576")  * @var file\irbundle\entity\file $file  */ public $file;    /**  * @orm\column(type="string", length=255)  */ public $title;   /**  * @orm\column(type="string", length=125)  */ public $impulseresponsetype;  /**  * @orm\column(type="string", length=125)  */ public $impulsesource;  /**  * @orm\column(type="string", length=555, nullable=true)  */ public $softwareused;  /**  * @orm\column(type="string", length=3)  */ public $bitdepth;  /**  * @orm\column(type="integer", length=12)  */ public $samplerate;  /**  * @orm\column(type="string", length=12)  */ public $impulseformat;  /**  * @orm\column(type="string", length=125, nullable=true)  */ public $microphoneused;  /**  * @orm\column(type="string", length=125, nullable=true)  */ public $preampused;  /**  * @orm\column(type="string", length=255, nullable=true)  */ public $location;  /**  * @orm\column(type="string", length=555, nullable=true)  */ public $tags;  /**  * @orm\column(type="text", nullable=true)  */ public $description;  /**  * @orm\column(type="string", length=255, nullable=true)  */ public $path;   /**  * @orm\manytoone(targetentity="account\userbundle\entity\user", inversedby="id")  * @orm\joincolumn(name="user_id", referencedcolumnname="id")  */  public $user_id;  /**  * @var \datetime  *  * @orm\column(name="date", type="datetime")  */ public $date;  public function setid($id) {     $this->id = $id;      return $this; }  /**  * id  *  * @return integer   */ public function getid() {     return $this->id; }  /**  * set title  *  * @param string $title  * @return file  */ public function settitle($title) {     $this->title = $title;      return $this; }  /**  * title  *  * @return string   */ public function gettitle() {     return $this->title; }  /**  * set impulseresponsetype  *  * @param string $impulseresponsetype  * @return file  */ public function setimpulseresponsetype($impulseresponsetype) {     $this->impulseresponsetype = $impulseresponsetype;      return $this; }  /**  * impulseresponsetype  *  * @return string  */ public function getimpulseresponsetype() {     return $this->impulseresponsetype; }  /**  * set impulsesource  *  * @return string  */ public function setimpulsesource($impulsesource) {     $this->impulsesource = $impulsesource;      return $this; }  /**  * impulsesource  *  * @return  string  */ public function getimpulsesource() {     return $this->impulsesource; }  /**  * set softwareused  */ public function setsoftwareused($softwareused) {     $this->softwareused = $softwareused;      return $this; }  /**  * softwareused  */ public function getsoftwareused() {     return $this->softwareused; }  /**  * set bitdepth  */ public function setbitdepth($bitdepth) {     $this->bitdepth = $bitdepth;      return $this; }  /**  * bitdepth  */ public function getbitdepth() {     return $this->bitdepth; }  /**  * set samplerate  */ public function setsamplerate($samplerate) {     $this->samplerate = $samplerate;      return $this; }  /**  * samplerate  */ public function getsamplerate() {     return $this->samplerate; }  /**  * set impulseformat  */ public function setimpulseformat($impulseformat) {     $this->impulseformat = $impulseformat;      return $this; }  /**  * impulseformat  */ public function getimpulseformat() {     return $this->impulseformat; }  /**  * set microphoneused  */ public function setmicrophoneused($microphoneused) {     $this->microphoneused = $microphoneused;      return $this; }  /**  * microphoneused  */ public function getmicrophoneused() {     return $this->microphoneused; }  /**  * set preampused  */ public function setpreampused($preampused) {     $this->preampused = $preampused;      return $this; }  /**  * preampused  */ public function getpreampused() {     return $this->preampused; }  /**  * set tags  *  * @param string $tags  * @return file  */ public function settags($tags) {     $this->tags = $tags;      return $this; }  /**  * tag1  *  * @return string   */ public function gettags() {     return $this->tags; }  /**  * set description  *  * @param string $description  * @return file  */ public function setdescription($description) {     $this->description = $description;      return $this; }  /**  * description  *  * @return string   */ public function getdescription() {     return $this->description; }  /**  * set location  *  * @param string $location  * @return file  */ public function setlocation($location) {     $this->location = $location;      return $this; }  /**  * location  *  * @return string   */ public function getlocation() {     return $this->location; }   /**  * set username  *  * @param string $user_id  * @return file  */ public function setuser_id($user_id) {     $this->user_id = $user_id;      return $this; }  /**  * username  *  * @return string   */ public function getuser_id() {     return $this->user_id; }  /**  * set date  *  * @param \datetime $date  * @return file  */ public function setdate($date) {     $this->date = $date;      return $this; }  /**  * date  *  * @return \datetime   */ public function getdate() {     return $this->date; }  public function getwebpath() {     return null === $this->path         ? null         : $this->getuploaddir().'/'.$this->path; }  protected function getuploadrootdir() {     // absolute directory path uploaded     // documents should saved     return __dir__.'/../../../../web/'.$this->getuploaddir(); }  protected function getuploaddir() {     // rid of __dir__ doesn't screw     // when displaying uploaded doc/image in view.     return 'uploads/'; }  public function setfile(uploadedfile $file = null) {     $this->file = $file;     // check if have old image path     if (is_file($this->getabsolutepath())) {         // store old name delete after update         $this->temp = $this->getabsolutepath();     } else {         $this->path = 'initial';     } }  /**  * @orm\prepersist()  * @orm\preupdate()  */ public function preupload() {     if (null !== $this->getfile()) {         $this->path = $this->getfile()->guessextension();     } }  /**  * @orm\postpersist()  * @orm\postupdate()  */ public function upload() {     if (null === $this->getfile()) {         return;     }      // check if have old image     if (isset($this->temp)) {         // delete old image         unlink($this->temp);         // clear temp image path         $this->temp = null;     }      // must throw exception here if file cannot moved     // entity not persisted database     // uploadedfile move() method     $this->getfile()->move(         $this->getuploadrootdir(),         $this->id.'.'.$this->getfile()->guessextension()     );      $this->setfile(null); }  /**  * @orm\preremove()  */ public function storefilenameforremove() {     $this->temp = $this->getabsolutepath(); }  /**  * @orm\postremove()  */ public function removeupload() {     if (isset($this->temp)) {         unlink($this->temp);     } }  public function getabsolutepath() {     return null === $this->path         ? null         : $this->getuploadrootdir().'/'.$this->id.'.'.$this->path;    } } 

apologies it's long. i've had uploading id numbers, can't work again, nor can working filenames, i've had few close calls far am. upload dir's set chmod of 777 recursive

thanks in advance

my advice follow symfony cookbook's example of uploading files seen here: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html or use pre-existing file uploading bundle. check out:

  1. vichuploaderbundle: https://github.com/dustin10/vichuploaderbundle
  2. doctrineextensions: https://github.com/l3pp4rd/doctrineextensions (uploadable part of bundle)

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -