简体   繁体   English

使用PHP存储多个文件的路径

[英]Storing the path of multiple files using PHP

I am working on an application, where the user has to upload files in a cart.eg the user upload the file "A" and now doing different work. 我正在开发一个应用程序,用户必须在购物车中上传文件。例如,用户上传文件“ A”,现在正在做其他工作。 After some time he again upload another file, file "B". 一段时间后,他再次上载另一个文件“ B”。 How I can manage the file path or store the file path, as if I use move_uploaded_file() function, then it can overwrite the other user's file with same file name. 我如何管理文件路径或存储文件路径,就像我使用move_uploaded_file()函数一样,它可以用相同的文件名覆盖其他用户的文件。

Thanks. 谢谢。

When I've had this issue, I have used a timestamp added to the filename. 遇到此问题时,我使用了添加到文件名的时间戳。 Usually I want to cleanse the filename anyway, so I 通常我还是想清理文件名,所以我

  1. replace characters I don't like 替换我不喜欢的字符
  2. remove the file extension and check it looks OK (eg pdf not exe) 删除文件扩展名,并检查它看起来是否正常(例如,pdf不是exe)
  3. add a timestamp to the filename 在文件名中添加时间戳
  4. put the extension back on 放回扩展

Obviously, this isn't suitable in every instance, but it might give you some ideas. 显然,这并不适合每种情况,但可能会给您一些想法。

You use php's time function to generate a timestamp that you append to the filename so that they can be different. 您可以使用php的time函数生成一个时间戳,该时间戳会附加到文件名中,以便它们可以不同。 Then you can use a column in the db to store the file paths. 然后,您可以在db中使用一列来存储文件路径。 You could store all the file paths in the same column but separate each one with ; 您可以将所有文件路径存储在同一列中,但用;分隔每个文件路径。 or any other character. 或其他任何字符。 To get the separate paths, you can use php's explode function. 要获取单独的路径,可以使用php的explode函数。

Create folder run time against session id, that way only current session user files goes to the folder. 根据会话ID创建文件夹运行时间,这样仅当前会话用户文件会进入该文件夹。

temp_uploads/ (main uploads folder)

temp_uploads/_jhk43543h5h435k3453 (session id folder for user 1)

temp_uploads/_jhk43543h5h435k34tr (session id folder for user 2)

temp_uploads/_jhk43543h5h43trtrtg (session id folder for user 3)

you just need store session id for each user, which you maybe you are already doing. 您只需要每个用户的存储会话ID,您可能已经在执行此操作。

happy Coding :) 快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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