Symfony2 - Where should I put files uploaded by us

2020-07-11 10:22发布

问题:

I am new with Symfony and I put images uploded by users under /web/bundles/mybundle/images/ directory. But I feel it's not good practice. Because when I run command below to create symlinks to other files under src directory. All images uploaded by users were removed.

php app/console assets:install ./web --symlink

Where should I put images uploaded by users?

回答1:

My personal solution is to put them in the /app/data/uploads directory, and then either:

  1. Create symlinks to the upload directory, or
  2. Serve the uploads via a controller

Option 1 is probably your best bet for images and assets, as you want them served as quickly as possible. However, for other content (PDFs, Docs, etc), option 2 might be your better bet, as you have more control over how you want the content served.

I prefer to keep my /web directory as clean as possible, and keep file uploads outside of public scope (allowing me to change my method of serving the files without needing to move them -- IE Moving from Option 1 above to Option 2), but if you want to store them in the web folder, you can follow the Symfony Cookbook and store them in something like /web/uploads.