StofDoctrineExtensionsBundle可上传

时间:2016-07-05 19:25:25

标签: symfony stofdoctrineextensions

我使用StofDoctrineExtensionsBundle Uploadable上传用户实体中的图片。

<?php

namespace Application\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @Gedmo\Uploadable(pathMethod="getPath", filenameGenerator="SHA1", allowOverwrite=true, maxSize="100000", allowedTypes="image/jpeg,image/pjpeg,image/png,image/x-png")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...

    /**
    * @ORM\Column(name="picture", type="string", length=255, nullable=true)
    * @Gedmo\UploadableFilePath
    */
    private $picture;

    public function getPath()
    {
        return '/user';
    }

    public function setPhoto($photo)
    {
        $this->photo = $photo;

        return $this;
    }

    public function getPhoto()
    {
        return $this->photo;
    }


    ...

在控制器中:

...

$em = $this->getDoctrine()->getManager();
$em->persist($user);

$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($user, $user->getPath());

...
FormType中的

...

->add('picture', FileType::class, array(
    'label' => 'Picture',
    'required' => false
))

...

config.yml:

# StofDoctrineExtensionsBundle Configuration
stof_doctrine_extensions:
    default_locale: fr_FR
    uploadable:
        # Default file path: This is one of the three ways you can configure the path for the Uploadable extension
        default_file_path:       %kernel.root_dir%/../web/uploads
        # Mime type guesser class: Optional. By default, we provide an adapter for the one present in the HttpFoundation component of Symfony
        mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter

        # Default file info class implementing FileInfoInterface: Optional. By default we provide a class which is prepared to receive an UploadedFile instance.
        default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo
    orm:
        default:
            uploadable: true

当我测试它时,我收到消息:

  

无法创建&#34; / user&#34; 。目录

任何想法都可以解决这个问题。感谢

1 个答案:

答案 0 :(得分:1)

您的应用是否在服务器中?如果是,请验证chmod。

或删除开头的/(如果您的文件夹结构为web/user):

public function getPath()
{
    return '/user';
}