php - File uploads in Symfony 2 using sonata-project -
i trying upload image in symfony 2 backend using sonata-project. challenge i'm facing right image inserted database table , uploaded specified path. have given upload folder writeable permissions. not sure missing here steps have taken.
class homeenumerate { // ... protected function getuploaddir() { return 'uploads/houses'; } protected function getuploadrootdir() { return __dir__.'/../../../../web/'.$this->getuploaddir(); } public function getwebpath() { return null === $this->house_picture ? null : $this->getuploaddir().'/'.$this->house_picture; } public function getabsolutepath() { return null === $this->house_picture ? null : $this->getuploadrootdir().'/'.$this->house_picture; }
}
i have edited homeenumerate.orm.yml have put in lifecyclecallbacks:
lifecyclecallbacks: prepersist: [ preupload, setcreatedatvalue, setexpiresatvalue ] preupdate: [ preupload, setupdatedatvalue ] postpersist: [ upload ] postupdate: [ upload ] postremove: [ removeupload ]
after generate:entities have edited homeenumerate entity
class job { // ... /** * @orm\prepersist */ public function preupload() { if (null !== $this->file) { $this->logo = uniqid().'.'.$this->file->guessextension(); } } /** * @orm\postpersist */ public function upload() { if (null === $this->file) { return; } // if there error when moving file, exception // automatically thrown move(). prevent // entity being persisted database on error $this->file->move($this->getuploadrootdir(), $this->logo); unset($this->file); } /** * @orm\postremove */ public function removeupload() { if(file_exists($file)) { if ($file = $this->getabsolutepath()) { unlink($file); } } }
}
can please me uploading files db , folder path.
you should implement callbacks in admin class, too. actually, sonataadminbundle has a cookbook recipe on handling file uploads. hopefully, should answer of questions.
Comments
Post a Comment