2016-07-05 32 views
1

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

<?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 

当我测试它,我得到的消息:

无法创建“/ user”目录。

任何想法来解决这个问题。谢谢

回答

1

你的应用程序在服务器?如果是这样,请验证chmod。

或者在(如果你的文件夹结构web/user)开始删除/

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

没有,我仍然在当地工作。 –

+0

好的。感谢它的工作。我在开始时删除了/ –