where is the best place to save images from users upload

前端 未结 5 424
挽巷
挽巷 2020-11-27 11:37

I have a website that shows galleries. Users can upload their own content from the web (by entering a URL) or by uploading a picture from their computer.

I am storin

相关标签:
5条回答
  • 2020-11-27 12:10

    While uploading images you need to verify the content of the file before uploading it. The file extension method is not trustable.

    Use magic number method to verify the file content which will be an easy way.

    See the stackoverflow post and see the list of magic numbers

    One way of saving the file is converting it to binary format and save in our database and next method is using App_Data folder.

    The storage option is based on your requirement. See this post also

    Set upload limit by setting maxRequestLength property to Web.Config like this, where the size of file is specified in KB

    <httpRuntime maxRequestLength="51200" executionTimeout="3600" />
    
    0 讨论(0)
  • 2020-11-27 12:24

    You can save your trusted data just in parallel of htdocs/www folder so that any user can not access that folder. Also you can add .htaccess authentication on your trusted data (for .htaccess you should kept your .htpasswd file in parallel of htdocs/www folder) if you are using apache.

    0 讨论(0)
  • 2020-11-27 12:33

    You should NOT store the user uploads anywhere they can be directly accessed by a known URL within your site structure. This is a security risk as users could upload .htm file and .js files. Even a file with the correct extension can contain malicious code that can be executed in the context of your site by an authenticated user allowing server-side or client-side attacks.

    See for example http://www.acunetix.com/websitesecurity/upload-forms-threat.htm and What security issues appear when users can upload their own files? which mention some of the issues you need to be aware of before you allow users to upload files and then present them for download within your site.

    1. Don't put the files within your normal web site directory structure

    2. Don't use the original file name the user gave you. You can add a content disposition header with the original file name so they can download it again as the same file name but the path and file name on the server shouldn't be something the user can influence.

    3. Don't trust image files - resize them and offer only the resized version for subsequent download

    4. Don't trust mime types or file extensions, open the file and manipulate it to make sure it's what it claims to be.

    5. Limit the upload size and time.

    0 讨论(0)
  • 2020-11-27 12:34

    One way is to store the image in a database table with a varbinary field.

    Another way would be to store the image in the App_Data folder, and create a subfolder for each user (~/App_Data/[userid]/myImage.png).

    For both approaches you'd need to create a separate action method that makes it possible to access the images.

    0 讨论(0)
  • 2020-11-27 12:35

    Depending on the resources you have to implement something like this, it is extremely beneficial to store all this stuff in Amazon S3.

    Once you get the upload you simply push it over to Amazon and pop the URL in your database as you're doing with the other images. As mentioned above it would probably be wise to open up the image and resize it before sending it over. This both checks it is actually an image and makes sure you don't accidentally present a full camera resolution image to an end user.

    Doing this now will make it much, much easier if you ever have to migrate/failover your site and don't want to sync gigabytes of image assets.

    0 讨论(0)
提交回复
热议问题