Upload image in Symfony 4 Upload image in Symfony 4 symfony symfony

Upload image in Symfony 4


I had the same problem, and I found out that it was a matter of permissions, if someone else got stuck in the part, give the folder all permissions and try it again.

Hope it could be helpful.


I recommand you to use the StofDoctrineExtensionsBundle: https://symfony.com/doc/master/bundles/StofDoctrineExtensionsBundle/index.html

The uploadable extensions is for you, is help you to upload file with some services.

In first, require it: composer require stof/doctrine-extensions-bundle
Now, configure it in the file config/packages/stof_doctrine_extensions.yml like that (considering the folder public/uploads is created with read/write permission):

stof_doctrine_extensions:    default_locale: en_US    uploadable:        default_file_path: "%kernel.root_dir%/../public/uploads"        mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter        default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo    orm:        default:            uploadable: true

Now, create the Entity/File.php for use it:

<?phpnamespace App\Entity;use Gedmo\Mapping\Annotation as Gedmo;use Doctrine\ORM\Mapping as ORM;use JMS\Serializer\Annotation as Serializer;use Overblog\GraphQLBundle\Annotation\GraphQLColumn;/** * @ORM\Entity * @Gedmo\Uploadable(filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true) */class File{    /**     * @ORM\Column(name="id", type="integer")     * @ORM\Id     * @ORM\GeneratedValue(strategy="IDENTITY")     */    protected $id;    /**     * @ORM\Column(name="path", type="string")     * @Gedmo\UploadableFilePath     */    protected $path;    /**     * @ORM\Column(name="name", type="string")     * @Gedmo\UploadableFileName     */    protected $name;    /**     * @ORM\Column(name="mime_type", type="string")     * @Gedmo\UploadableFileMimeType     */    protected $mimeType;    /**     * @ORM\Column(name="size", type="decimal")     * @Gedmo\UploadableFileSize     */    protected $size;    /**     * @GraphQLColumn(type="String")     */    protected $publicPath;    /**     * @return string     */    public function getPublicPath()    {        return '/uploads/'.$this->name;    }    /**     * @return mixed     */    public function getId()    {        return $this->id;    }    /**     * @return mixed     */    public function getPath()    {        return $this->path;    }    /**     * @param mixed $path     *     * @return File     */    public function setPath($path)    {        $this->path = $path;        return $this;    }    /**     * @return mixed     */    public function getName()    {        return $this->name;    }    /**     * @param mixed $name     *     * @return File     */    public function setName($name)    {        $this->name = $name;        return $this;    }    /**     * @return mixed     */    public function getMimeType()    {        return $this->mimeType;    }    /**     * @param mixed $mimeType     *     * @return File     */    public function setMimeType($mimeType)    {        $this->mimeType = $mimeType;        return $this;    }    /**     * @return mixed     */    public function getSize()    {        return $this->size;    }    /**     * @param mixed $size     *     * @return File     */    public function setSize($size)    {        $this->size = $size;        return $this;    }}

The last step is to create a relation the File entity. After that, you can concretize the upload using the UploadManager like that:

<?phppublic function uploadFile(\Stof\DoctrineExtensionsBundle\Uploadable\UploadableManager $uploadableManager){    $uploadableManager->markEntityToUpload($file, $fileInfo);}

And just persist/flush it.