PHP: Saving file locations ... what if they are overwritten?

I am currently using the Zend Framework and have a form for uploading files. An authenticated user has the ability to upload a file that will be stored in a directory in the application, and in a location stored in the database. Thus, it can be displayed as a file that can be downloaded.

<a href="/upload-location/filename.pdf">Download</a>

But what I notice is that a file with the same name will overwrite the file in the uploads directory. There is no error message, and the file name does not increase. Therefore, I think the file should be overwritten (or never loaded).

What are some guidelines I should know when loading, moving or storing these files? Should I always rename files so that the file name is always unique?

+3
source share
3 answers

As a rule, we do not store files with the name given by the user, but using the name that we (i.e. our application) chosse.

For example, if a user downloads my_file.pdf, we should:

  • save a row in the database containing:
    • id; auto increment, primary key is " 123", for example
    • user name so we can send the correct name when someone tries to upload a file.
    • file content type; application/pdfor something like this, for example.
    • "our" name: file-123for example
  • id=123, , ('file-' . $id) .
  • "" , , , " "
  • content, btw

, :

  • "" , ,
  • : , .
+9

:

id , . /somedir/part1ofID/part2OfID , /somedir/theWholeID, , , , .

: script, , , . , , , - .

. , .

+1

, . , - orignal, pk db , - , varous db, .

0

Source: https://habr.com/ru/post/1725316/


All Articles