【问题标题】:Symfony2 FOSUserBundle user picture UploadSymfony2 FOSUserBundle 用户图片上传
【发布时间】:2014-10-27 10:55:24
【问题描述】:

我正在尝试使用 FOSUserBundle 在 symfony2 项目中向我的用户实体添加个人资料图片。我已经使用过 symfony2 文件上传过程,当我使用 FOSUserBundle 时,我只有上传和数据库持久性问题。

这是我的实体:

namespace ICGM2\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @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) 
 */
public $path;

/**
 * @Assert\File(maxSize="6000000") 
 */
public $file;

/*     * *************************************************************** */

/**
 * @ORM\PrePersist() 
 * @ORM\PreUpdate() 
 */
public function preUpload()
{
    if (null !== $this->file) {
        $this->path = uniqid('', true) . '.' . $this->file->guessExtension();
    }
}

/**
 * @ORM\PostPersist() 
 * @ORM\PostUpdate() 
 */
public function upload()
{
    if (null === $this->file) {
        return;
    }
    $this->file->move($this->getUploadRootDir(), $this->path);
    unset($this->file);
}

/**
 * @ORM\PostRemove() 
 */
public function removeUpload()
{
    if ($file == $this->getAbsolutePath()) {
        unlink($file);
    }
}

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

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

protected function getUploadRootDir()
{
    return __DIR__ . '/../../../../web/' . $this->getUploadDir() . '/' . $this->id;
}

protected function getUploadDir()
{
    return 'uploads/users';
}

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

    return $this;
}

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

....

还有表单:UserType.php

  $builder->add('file', 'file', array(
        'required' => false
    ));

【问题讨论】:

    标签: symfony


    【解决方案1】:

    嗨,如果有人在这里遇到同样的问题,他的解决方案。 我更改了用户实体中的一些功能:

       /**
     * @param UploadedFile $file
     * @return object
     */
    public function setFile(UploadedFile $file = null)
    {
        // set the value of the holder
        $this->file = $file;
        // check if we have an old image path
        if (isset($this->path)) {
            // store the old name to delete after the update
            $this->t = $this->profilePicturePath;
            $this->tempPath = null;
        } else {
            $this->path = 'initial';
        }
    
        return $this;
    }
    
    /**
     * Get the file used for profile picture uploads
     * 
     * @return UploadedFile
     */
    public function getFile()
    {
    
        return $this->file;
    }
    
    /**
     * @ORM\PrePersist() 
     * @ORM\PreUpdate() 
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // a file was uploaded
            // generate a unique filename
            $filename = $this->generateRandomProfilePictureFilename();
            $this->setPath($filename . '.' . $this->getFile()->guessExtension());
        }
    }
    
    /**
     * Generates a 32 char long random filename
     * 
     * @return string
     */
    public function generateRandomProfilePictureFilename()
    {
        $count = 0;
        do {
            $generator = new SecureRandom();
            $random = $generator->nextBytes(16);
            $randomString = bin2hex($random);
            $count++;
        } while (file_exists($this->getUploadRootDir() . '/' . $randomString . '.' . $this->getFile()->guessExtension()) && $count < 50);
    
        return $randomString;
    }
    
    /**
     * @ORM\PostPersist() 
     * @ORM\PostUpdate() 
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }
        $this->getFile()->move($this->getUploadRootDir(), $this->getPath());
    
        if (isset($this->tempPath) && file_exists($this->getUploadRootDir() . '/' . $this->tempPath)) {
            unlink($this->getUploadRootDir() . '/' . $this->tempPath);
            $this->tempPath = null;
        }
        $this->file = null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多