symfony - Symfony2 FOSUserBundle user picture Upload -


I'm trying to add a profile picture in your user unit that using Fossusrbndl a Simfoni 2 project. I have already worked with the Simplified 2 file upload process, when I use FosurBundle, then there are only one upload and database problems.

Here's my unit:

  Namespace ICGM2 \ User Bundle \ unit; Use FOS \ UserBundle \ Entity \ User as BaseUser; Use the theory / ORM \ mapping as an ORM; Use the principle \ mango \ collection \ ArrayCollection; As the Symfony \ Component \ Validator \ Constraints use; Use Symfony \ Component \ HttpFoundation \ File \ UploadedFile; / ** * @ORM \ Entity * @ORM \ Table (name = "user") * / class user expands Besusr {/ ** * @ORM \ Id * @ORM \ column (type = "integer") * @ORM \ value generated (strategy = "auto") * / protected $ id; / ** * @ORM \ column (type = "string", length = 255, faucet = true) * / public $ path; / ** * @ Essar \ file (maximization = "6000000") * / public $ file; / * * ************************************************************************************************* **************************************************************************************************** ************** * / / ** * {@ORM \ Preepersist () * @ORM \ PreUpdate () * / public function preUpload () if (null! == $ it- & Gt; File) {$ this- & gt; Path = uniqid ('', is true). '.' . $ This- & gt; File & gt; GuessExtension (); }} / ** * @ORM \ PostPersist () * @ORM \ PostUpdate () * / Public Function Upload () {if (null === $ this- & gt; file) {Return; } $ This- & gt; File-> Move ($ this- & gt; getUploadRootDir (), $ this- & gt; path); Not set ($ this-> File); } / ** * @ORM \ PostRemove () * / Delete Public Function () {if ($ file == $ this-> GetAbsolutePath ()) {Unlink ($ file); }} Public function getAbsolutePath () {return null === $ this-> Path? Free: $ this- & gt; GetUploadRootDir () '/' $ this- & gt; Path; } Public function getWebPath () {return tap === $ this- & gt; Path? Free: $ this- & gt; GetUploadDir () '/' $ this- & gt; ID '/' $ this- & gt; Path; } Secure function getUploadRootDir () {back __DIR__. '/../../../../web/' $ this- & gt; GetUploadDir () '/' $ this- & gt; Id; } Secure function getUploadDir () {Return 'Upload / User'; } / ** * Set path * * @ Ultimate string $ path * @ return user * / Public function set path ($ path) {$ this- & gt; Path = $ path; $ This return; } / ** * Get path * * @ layer string * / public function getPath () {return $ this- & gt; Path; }  

....

More Form: UserType.php

  $ Builder-> Add ('file', 'file', array ('required' = & gt; wrong));  

Hello, if somebody has solved its problem, I change some functions in the user unit have said:

  / ** * @ param Aplodedfail $ file * @ return object * / public function Setfail (Aplodedfail $ file = null) {// set the value holder $ $ - this - & gt; File = $ file; // Check if we have an old image path (if isset ($ this-> path)) // Update after updating to delete the old name- $$ -> = $ this-> ProfilePicturePath; $ This- & gt; TempPath = null; } And {$ this- & gt; Path = 'initial'; } $ This return; } / ** * Get a file to upload to profile photo * * @ Return uploaded file * / Public function getFile () {$ $ this- & gt; Return file; } / ** * @ORM \ PrePersist () * @ORM \ PreUpdate () * / public function preUpload () {if (null! = $ $ This- & gt; getFile ()) was uploaded {// file // Create a unique filename $ $ filename = $ this-> GenerateRemove ProfilePhotam name (); $ This- & gt; Setpath ($ filename. '.' $ This-> getFile () - & gt; Estimate Extension ()); }} / ** * Random file name returning a 32-character long * * @ generates strings * / public functionsRendFrintFileFilmFilename () {$ count = 0; {$ Generator = new safe reserve (); $ Random = $ generator- & gt; Next bows (16); $ RandomString = bin2hex ($ Random); $ Count ++; } While $ (file_exists ($ this- & gt; getUploadRootDir (). '/'. $ Random string. '.' $ This- & gt; getFile () - & gt; estimate approximate ()) & amp; amp; Amp; amp; $ count & lt; 50); Return $ randomString; } / ** * @ORM \ PostPersist () * @ORM \ PostUpdate () * / Public Function Upload () {if (null === $ this- & gt; file) {return; } $ This- & gt; GetFile () - & gt; Move ($ this-> getUploadRootDir (), $ this- & gt; getPath ()); If (isset ($ this-> tempPath) & amp; amp; and file_exists ($ this-> getUploadRootDir (.) / '. $ This- & gt; tempPath)) {unlink ($- >-- ; GetUploadRootDir () '/'. $ This-> tempPath); $ This- & gt; TempPath = null; } $ This- & gt; File = null; }  

Comments