Storing uploaded files on the server

3796 views php
0

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)

answered question

1 Answer

11

  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 file to download by the HTTP headers (header function): header('Content-Disposition: inline; filename="original name.pdf"');
  4. You should store files in different directories when you are going to save millions of files. Each new file in the directory decreases the performance of I\O operations.

posted this

Have an answer?

JD

Please login first before posting an answer.