在数据库中存储上传照片信息的指南
我们正在为客户制作一个社交网站(课程的最终项目),他想要一个照片上传功能。
我们考虑在 MySQL 数据库中放置一个指向图片的链接,其中包含图片的唯一 ID 以及用户 ID 的外键。
但我想知道什么是最安全的方法。
- 我们应该保留图片名称还是重命名它?
- 我们应该将所有图片保存在同一个文件夹中,还是为每个唯一用户 ID 建立一个单独的文件夹?
- 如果我们重命名图片,是否应该从图片的唯一 ID 开始? (1 到 XXXX)
安全:使用恶意文件名进行任何类型的攻击 最快:先有 1 个文件夹,然后是 XXXXX 文件夹
We are making a social site for a client (final project for classes) and he wants a photo uploading feature.
We thought about putting a link in a MySQL database to the picture with a unique ID for the picture and also a foreign key to the User ID.
But I was wondering what would be the safest method.
- Should we keep the picture name or rename it?
- Should we keep all pictures within the same folder, or have a separate folder for each Unique User ID?.
- If we rename the picture, should we just start with the unique ID for the picture? (1 to XXXX)
Safe : any type of explointing with a malicious filename
Fastest : to have 1 folders then XXXXX folder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于上传的图像,我会将图像重命名为 userid-imageid,以便将图像命名为 123-5554.jpg 例如,这将按用户 ID 对它们进行分组,同时将它们保留在同一文件夹中(使用排序),并提供唯一的名称对于每个图像。
如果您不重命名图像,有人可以轻松多次上传名为 picture.jpg 的图像。
For uploaded images I would rename the image to the userid-imageid so an image would be named 123-5554.jpg for example, this would group them by userid while keeping them in the same folder (using sorting), and provide a unique name for each image.
If you dont rename the image, someone could easily upload a image called picture.jpg more than once.
我会将每张图片的文件名更改为唯一的名称。每张图片在表中也应该有一个唯一的 ID。然后你可以在图片的唯一id上设置一个外键到用户的id。
你的第二个问题是你自己的偏好,取决于你想要的结构类型。我会为每个用户创建一个单独的文件夹,如果有大量数据,它会更直观且更容易导航。
I would change each pictures filename to something unique. Each picture should have a unique id in the table as well. Then you can set a foreign key on the picture's unique id to the user's id.
Your second question is kind of your own preference, depends on the kind of structure you would want to have. I would create a separate folder for each user, its more intuitive and a little easier to navigate if there is a lot of data.