简体   繁体   中英

Storing uploaded files on the server

I have no idea how to store uploaded files on my server. For example, should I always rename the uploaded file to a random string like 458783576487365934 and always store the files in the same folder? For example if I store pdf documents or images, do I put all the pdfs in the same folder (but with a random name so I can upload documents with same name)? If I do so, I would like that when the user goes to the url /files/458783576487365934 for instance, the downloaded file has its original name and not the real stored name but how?

I can store in DB the path and the original name but how to tell the browser its real name when we go to /files/458783576487365934 ?

(I don't care about the actual process of uploading, I know about $_FILES and I use Symfony anyway)

  1. You should store original name in the database.
  2. You should not store files with original name, it's insecure. Use server-generated names.
  3. You can tell browser the name of the file with a help of HTTP headers ( header function):

header('Content-disposition: inline; filename="original_name.pdf"');

  1. You should store files in different directories when you are going to keep millions of files. Each new file in the directory decreases the performance of I\\O operations when you try to access the data inside the directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM