2014-07-14 82 views
1

我正在使用Symfony2.3 + Sonata Admin + FosUserBundle + SonataUserBundle。 Everthing工作正常。FOS用戶文件上傳

用戶:我已經創建的用戶圖片上傳功能,這是偉大的工作,當用戶上傳自己的圖片,然後上傳路徑

/web/uoloads/documents/imagesname.jpg 

我想改變這條道路和S3上的所有用戶上傳的圖像。

這是用戶實體: -

/** 
* @ORM\Entity 
* @ORM\Table(name="user") 
*/ 
class User extends BaseUser 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string", length=255, nullable=true) 
*/ 
protected $path; 

/** 
* @Assert\Image(maxSize="6000000", mimeTypesMessage="Please select a valid image") 
*/ 
private $file; 

public function __construct() 
{ 
    parent::__construct(); 

} 

public function getAbsolutePath() 
{ 
    return null === $this->path 
     ? null 
     : $this->getUploadRootDir().'/'.$this->path; 
} 

public function getWebPath() 
{ 
    return null === $this->path 
     ? null 
     : $this->getUploadDir().'/'.$this->path; 
} 

protected function getUploadRootDir() 
{ 
    // the absolute directory path where uploaded 
    // documents should be saved 
    return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
} 

protected function getUploadDir() 
{ 
    // get rid of the __DIR__ so it doesn't screw up 
    // when displaying uploaded doc/image in the view. 
    return 'uploads/documents'; 
} 


/** 
* Sets file. 
* 
* @param UploadedFile $file 
*/ 
public function setFile(UploadedFile $file = null) 
{ 
    $this->file = $file; 
} 

/** 
* Get file. 
* 
* @return UploadedFile 
*/ 
public function getFile() 
{ 
    return $this->file; 
} 

public function upload() 
{ 
// the file property can be empty if the field is not required 
if (null === $this->getFile()) { 
    return; 
} 

// use the original file name here but you should 
// sanitize it at least to avoid any security issues 

// move takes the target directory and then the 
// target filename to move to 
$this->getFile()->move(
    $this->getUploadRootDir(), 
    $this->getFile()->getClientOriginalName() 
); 

// set the path property to the filename where you've saved the file 
$this->path = $this->getFile()->getClientOriginalName(); 

// clean up the file property as you won't need it anymore 
$this->file = null; 
} 

/** 
* Set path 
* 
* @param string $path 
* @return Image 
*/ 
public function setPath($path) 
{ 
    $this->path = $path; 

    return $this; 
} 

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

} 

這樣Get service container from entity in symfony 2.1 (Doctrine)沒有工作,在FOS用戶顯示的錯誤,我改變通過服務容器,這個路徑的實體,但在正常通話服務conatiner。

我很困惑。任何一個人都建議我如何改變這條道路並轉向s3。如果在User Entity中成功調用服務容器,那麼我的問題就解決了,但是正常的symfony服務調用在FOS User Bundle中不起作用。

謝謝!

回答

0

爲什麼不使用VichUploaderBundle?與Flysystem/Gaufrette相關聯,很容易將上傳的文件鏈接到實體,並將它們發送到S3,一個FTP,無論如何。

+0

謝謝你回答,這個項目目前正在運行,所以我不想改變洞的東西。 – Sid