1

I am using Symfony2.3 + Sonata Admin + FosUserBundle+SonataUserBundle. Everthing is working fine.

In User: I have created user image upload function this is working great and when User upload his image then upload path

/web/uoloads/documents/imagesname.jpg

I want to change this path and all user images uploads on s3.

This is User Entity :-

/**
 * @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;
}

}

I change this path through service container in entity but in normal calling service conatiner like this Get service container from entity in symfony 2.1 (Doctrine) not working and show error in FOS User.

I am confuse . Any one suggest me How I can change this path and move to s3. If successfully call service container in User Entity then my problem is solve but normal symfony service calling is not working in FOS User Bundle.

Thanks!

Community
  • 1
  • 1
Kunwar Siddharth Singh
  • 1,676
  • 6
  • 33
  • 66

1 Answers1

0

Why don't you use VichUploaderBundle? Associated with Flysystem/Gaufrette, it's easy to link uploaded files to entities and send them to S3, a FTP, whatever.

K-Phoen
  • 1,260
  • 9
  • 18