I use the StofDoctrineExtensionsBundle Uploadable to upload a picture in User entity.
<?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;
}
...
In the controller:
...
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($user, $user->getPath());
...
in the 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
When I test it I get the message:
Unable to create "/user" directory.
Any idea to solve this problem. Thanks
Is your app in a server ? If so, verify the chmod.
Or remove the /
at the beginning of (if your folder structure is web/user
):
public function getPath()
{
return '/user';
}