All about Uploading Files in Symfony

4:39:58

What you'll be learning

Need to upload some files in Symfony? Excellent! You've come to the right place!

These days, uploading files can be simple... or incredibly complex - with cloud storage, private files, thumbnailing, validation and so much more. So let's tackle... all of it!

  • Understanding basic file uploading
  • File uploads in a Symfony form
  • File (and Image) validation
  • Naming files (unique vs keeping the original name... or both?)
  • Handling/moving uploaded files in an organized way
  • Rendering links to the uploaded files
  • Thumbnailing with LiipImagineBundle
  • "Uploading" files in your data fixtures
  • Flysystem: using a filesystem abstraction
  • Uploading files but keeping them private
  • Adding a download endpoint for "private" files
  • Fancy AJAX uploading with Dropzone
  • Storing files in the cloud (S3)

So get into the (Drop)zone and let us upload some serious knowledge to you in this... multipart(.../form-data) tutorial. Ok... better jokes are inside... I promise!


Your Guides
Ryan Weaver

Buy Access
Login or register to track your progress!

Questions? Conversation?

  • 2019-04-11 Diego Aguiar

    Hey Skylar Scotlynn Gutman

    In theory your test folder should mirror your production folder (src/ directory). When doing TDD you must start with a test first. Usually in that phase is when you take some time to think about architecture, relation between objects, etc, but also it's recomended to code small tests, so you can develop the module or whatever you are doing little by little. TDD requires a lot of practice and a lot of discipline but if every time you code something new, you start from coding some tests, eventually you will get good at it. Probably you will find very interesting this "kata" from Uncle Bob: http://butunclebob.com/Arti...

    Cheers!

  • 2019-04-11 Skylar Scotlynn Gutman

    Thank you again for this tutorial!!!

    I know that you guys are super busy. But, I was wondering what would the tests folder look like if you did this using TDD. I know it would double the length of this course, and so you would not add it to the script. But there are many unique coding techniques and I would like to see your approach to building the test cases. You could leave them there as bonus material. Tests are a very weak area of my coding as I am usually under pressure to complete. But the apps always grow, and I regret not doing them sooner :(.

  • 2019-04-11 Skylar Scotlynn Gutman

    Wow guys!! This tut is awesome and so filled of knowledge. Thanks again.

  • 2019-03-07 ElGovanni

    Oh thanks Victor, I didn't knew about discount for students :)

  • 2019-03-07 Victor Bocharsky

    Hey ElGovanni,

    Hey, we're working on releasing a new chapter of this course every day. Because it's kinda big course - 33 chapters - it might be fully released in about a month I think, or maybe a bit faster, but not too much. Thank you for your patience! :)

    Btw, if you're a student - you're in luck! We provide discounts for students! Please, contact us directly to get more information about it.

    Cheers!

  • 2019-03-07 ElGovanni

    Do You have any final date of release? I'd to subscribe SC like Netflix but 25$ is like 100 in Poland, especially for student 😅

  • 2019-03-04 Diego Aguiar

    Thanks Christina Vandendyck! We're glad to hear you like our tutorials

    Cheers!

  • 2019-03-04 Christina Vandendyck

    Amazing lessons, like usual.
    I especially appreciate the way you write (I'm more the kind to read, I never watch videos), that makes me understand things really easily.

    Can't wait for the next part!

    Thankssss

  • 2019-02-26 weaverryan

    Hey Gregory Berger!

    Awesome tip! Indeed, it looks like if you wanted to programmatically make some changes to an image (like, not just via configuration, but inside a service - run a bunch of methods to transform the image), that is an awesome tool! And combined with Symfony Messenger (so you can async this stuff), that's a pretty killer combo. I will definitely mention it... at the very least :). We will definitely be talking about FlySystem and both the local and S3 adapter (and also a cache adapter).

    Cheers!

  • 2019-02-25 weaverryan

    Hey Дмитрий Ченгаев!

    I'm happy to say that we're working on some design changes right now that I *think* will include this - you're not the only person who has asked :).

    Cheers!

  • 2019-02-24 Gregory Berger

    weaverryan maybe would it be also great to mention that while LiipImagineBundle is a great tool, some other tools exist outside of the Symfony ecosystem. I'm thinking about Intervention, that comes whith Laravel helpers but could totally be used inside a Symfony application. (Same goes for custom FlySystem adapters). Anyway : thanks for this long awaited series, I have students who will be delighted ;)

  • 2019-02-23 Дмитрий Ченгаев

    As a wish, is it possible to add breadcrumbs over the course heading? It is difficult to determine which site category the course belongs to.

  • 2019-02-18 Sasa Milivojevic

    I thought that LiipImagineBundle is for resize picture, so I thought that you will cover that case. Anyway I cant wait to see the rest of videos.

    Wish you best!

  • 2019-02-18 weaverryan

    Hey Lopoi Popoi!

    Got it! Most was already in there - but I'm going to make sure this is all in there. For this part, it will probably be a list of "documents" (e.g. PDF, doc, images, whatever you want) that we will re-order, delete, rename, etc. I don't think it makes much difference - we'll just render the filename and a link to download it instead of an img tag (we will cover some image-specific things in another part of the tutorial).

    Cheers!

  • 2019-02-18 weaverryan

    It's totally going to be a part of it! :D :D :D + using with LiipImagineBundle + eventually using Flysystem to store everything in S3. FUN stuff :)

  • 2019-02-18 weaverryan

    I was thinking the same thing :). We'll talk about this. You DO need to bump this setting up, if you want large file uploads. But, if you setup validation correctly, you should at least get a nice validation error (and not some weird 500 error or something like that).

    Cheers!

  • 2019-02-18 Sasa Milivojevic

    upload_max_filesize 2M

  • 2019-02-18 Vladimir

    I think Flysystem will be interesting part of this course! weaverryan WDYT?

  • 2019-02-18 Lopoi Popoi

    Which type of error?
    It can be not Symfony-related error but server limitations.

    What is the value of upload_max_filesize option in php.ini file (you can check it in SF profiler toolbar)?

  • 2019-02-18 Sasa Milivojevic

    Hi Ryan!
    Now I use vich/uploader-bundle, and problem is when I want to upload image biger thah 5mb he give me error, so I must to download my picture from mobile phone to resize it, and then to upload.

  • 2019-02-18 weaverryan

    Hey Sasa Milivojevic!

    MAYBE. It depend what you mean - but I'd like to know more :). We WILL cover this case: the user can upload a file of any size, and then when you render them, you resize them to some specific dimensions - e.g. 200x200. What we are *not* currently planning on covering is something like: the user uploads a photo and then there is some JS widget that pops up to allow the user to dynamically resize/crop their photo.

    Let me know what you're looking for - and thanks for posting ideas here so early - very helpful!

    Cheers!

  • 2019-02-16 Lopoi Popoi

    Hi weaverryan!

    I mean exact as you mentioned:

    "When I am editing a Product, I am able to see all the photos for that
    product on that form, and am able to re-order, delete, upload more,
    rename, etc"

    Will something like that be in this course?

  • 2019-02-16 Sasa Milivojevic

    Are you going to cover picture resize?

  • 2019-02-15 weaverryan

    Hey Peter Kosak!

    It's actually a topic that I (so far) have been purposely avoiding in this tutorial. But let me explain - and then you can tell me if you disagree (because ultimately, people *are* asking for this, and the tutorial needs to cover all the stuff that people really need). Instead of true multiple file uploads, I much prefer a JavaScript approach - and we WILL cover this in the tutorial. For example, Dropzone allows you to select multiple files and they are both uploaded instantly. Well, *technically*, they are uploaded on two separate requests - so one file per request. But it accomplishes what a user needs: allowing you to upload multiple files at once. It's also a better user experience in my opinion, because the uploads have a progress bar, happen via AJAX, and you don't need to wait until you finish filling out the whole form before you submit (and then, if you have a validation error, the file uploads are lost).

    So, would that approach solve what you're looking for? Or is there something specific about native multiple file uploads?

    Thanks for the feedback!

  • 2019-02-15 weaverryan

    Hey Lopoi Popoi!

    Thanks for sharing :). One more question: What do you mean exactly by an "image gallery". I've had a few people as about this - but it can mean a lot of different things. For example, you could build a "widget" that allows you to randomly upload any image to your system, search, delete, etc - all from within maybe even a JS "modal". And you might even find some third-party JS library for this and then try to integrate it into Symfony. Or, you might just mean "When I am editing a Product, I am able to see all the photos for that product on that form, and am able to re-order, delete, upload more, rename, etc".

    Thanks!

  • 2019-02-15 Peter Kosak

    I would like to suggest multiple file upload

  • 2019-02-14 Lopoi Popoi

    Hey weaverryan!
    Example: user drops files and wants to change their sequence (position). For instance, make the last one - the first or something.
    Or open record (image gallery) containing images and delete one image and upload two more then save the record.
    Or change image (file) name in the gallery.
    I am thinking of embedded form collection , it can be pain-point.

  • 2019-02-14 weaverryan

    Hey Lopoi Popoi!

    Maybe... ;)

    Can you explain a bit more? What do you mean by "uploading multiple files" - are you building a form where you have multiple file upload inputs and you handle them all at once when you submit the form? Or do you have some JavaScript where you want to allow the user to, for example, drop one file and have it upload, then drop another file and have it upload (where each 1 upload is technically its own 1 request, but you could do multiple at a time)?

    And what do you mean by edit? If I upload a PDF... editing it would really mean deleting it and uploading a new one, right? Basically, describe the feature you're dreaming up - I definitely want to make sure it's included.

    Thanks!

  • 2019-02-13 Lopoi Popoi

    Hello symfonycasts team!
    Will you cover edit/delete options when uploading multiple files?

  • 2019-02-13 Leanna Pelham

    Thanks for the feedback Krzysztof Krakowiak we'll look into that! And this course will start releasing next week :)

  • 2019-02-13 Leanna Pelham

    Yep! We will start releasing this course next week :)

  • 2019-02-13 mouerr

    this course will be after phpSpec?

  • 2019-02-12 Krzysztof Krakowiak

    Ryan! When? :D And Please add breadcrumbs to SymfonyCasts, because at the moment navigation is a bit painful...