MSadura
MSadura

Reputation: 1042

Symfony 2 handling file uploads

I can't find straight answer to my question about handling file uploads. In general: what is the best "symfony" way to upload files, including good practises?

Below my research / doubts:

according to docs: handling file uploads , looks like I can have uploadFile object in my entity out of the box and manage it. The problem is that I dont feel like entity has something to do with that upload and it doeasnt seem like good place to handle it. I.e. I'd like to save my image files in different folders (according to upload date) and I dont like the idea of checking / creating folders and paths inside entity. Good side is that I have access to lifecycle callbacks to upload/ delete files.

After researching more I've found this topic: Symfony 2 - Best practice to upload an image on Amazon S3 Idea from that topic is to create custom handler for file uploads. I like that, especially because in my example I'd like to use extra services to resize uploaded images and I can easily inject them to handler. My problem here is: that topic is 2 years old so maybe in current symfony that kind of solution is not "viable" anymore?

Final question: which method is better in matter of good practises in symfony? Do I miss something? I hope that this question is not too general.

TLDR: What is best way to handle uploaded file in actyal version of symfony2 - hangling file as uploadedFile in entity, or move that logic to some custom handler/ service?

Upvotes: 0

Views: 1439

Answers (2)

George
George

Reputation: 1499

I started using OneupUploaderBundle with jQuery File Uploader. I needed to upload multiple images at the same time and since you mentioned images this may work for you. In the OneupUploaderBundle EventListener, you can add custom logic right after the file is uploaded which includes moving the image to any folder you want, resizing, and adding other filters. I setup a method that automatically resizes and creates a thumbnail image, and my entity only stores the file info and location. Check these out and see if it matches you needs any better.

https://github.com/1up-lab/OneupUploaderBundle

http://blueimp.github.io/jQuery-File-Upload/

Upvotes: 2

lsouza
lsouza

Reputation: 2488

Actually the nicest way I've found to manage uploadable fields on my entities is using VichUploaderBundle. It's easy to configure and play nice with Gaufrette if you are planning to use s3 or a similar service.

Here's a quick example on using the bundle:

Config file:

vich_uploader:
    db_driver: orm
    mappings:
        product_image:
            uri_prefix: images/products
            upload_destination: %kernel.root_dir%/../web/images/products
            namer: vich_uploader.namer_origname
            delete_on_remove: true

Your entity:

use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @Vich\Uploadable
 */
class Product
{
    /**
     * @Assert\File(
     *     maxSize="5M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="image")
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, name="image", nullable=true)
     */
    protected $image;
}

Upvotes: 2

Related Questions