(php) - 帮助编写上传图像的脚本
我正在尝试将图像上传到服务器上的目录。我正在使用 http://www.reconn.us/content 中找到的教程/view/30/51/。
首先,这是使用 PHP 上传图像的好方法吗?
其次,我还将把信息存储在 MySQL 数据库中。处理与用户上传的同名图像的好方法是什么?例如,如果用户连续上传文件“test.png”两次,那么第二个文件名会发生什么情况?从上面的脚本中,两者都将获得唯一的文件名,但作为用户我将如何再次访问该图像?我不能只是查询,因为我知道的唯一名称是我给它的重复名称,而且我绝对不知道服务器使用上传时间给它的唯一名称......
第三,什么是好的最大文件大小对于图像?
I am trying to upload an image to a directory on a server. i'm using the tutorial found at http://www.reconn.us/content/view/30/51/.
First, is that a good method for uploading images using PHP?
Second, I'm also going to store the info in a MySQL database. What is a good way to deal with images that have the same name that the user uploads? For example, if a user uploads a file 'test.png' 2x in a row, what should happen to the second filename? From the script above, both will get a unique filename, but how would I as the user access that image again? I couldn't just query because the only name I know was the duplicate name I gave it, and I definitely don't know the unique name the server gave it using the upload time...
Third, what is a good max file size for images?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在上传后向用户报告唯一的 URL,以便用户知道在哪里可以找到该图像。因此,第一个 test.png 可能是 http://www.example.com/images/fjdklagjsdl .jpg 第二个可能是 http://www.example.com/images /jklfsdlkj.jpg
您还可以为用户提供某种界面来查看他们上传的图像。如果您在图像的唯一文件名旁边显示上传图像的缩略图,用户将很容易识别哪个图像是哪个。
You can report the unique URL back to the user after the upload so that the user will know where to find the image. So, the first test.png could be http://www.example.com/images/fjdklagjsdl.jpg and the second could be http://www.example.com/images/jklfsdlkj.jpg
You can also provide some kind of interface for users to view images they've uploaded. If you display a thumbnail of the uploaded image next to the image's unique filename, it will be easy for the user to identify which image is which.
这是我使用的方法:
用户上传图像
服务器使用唯一的(GUID或其他)文件名保存图像并存储 - 唯一生成的文件名和原始上传的文件名在数据库中
图像使用
链接到
、图像
表的original_filenameunique_filename
或primary_key。图像从服务器获取,并使用数据库中存储的原始文件名提供服务。这样您就可以避免文件名冲突的机会,并保留图像的原始文件名。此外,这还允许您在
original_filename
列上构建搜索以供用户使用。使用此方法,无需向用户公开唯一的文件名,而是使用它们来查找与“images”中的特定
id
或original_filename
关联的图像桌子。当然,如果您不介意在显示图像时为其提供原始文件名,则可以在存储时生成唯一的文件名。
This is the method I use:
Users upload images
Server saves the image with a unique (GUID or something) filename and stores - both - the unique generated filename and the original uploaded filename in a database
Images are linked to using either the
original_filename
,unique_filename
or primary_key for theimages
table.The images are taken from the server, and served using the original filename stored in the database. This way you avoid chances of conflicting filenames and you preserve the image's original filename. In addition, this allows you to build a search on the
original_filename
column for the user to use.With this method, unique filenames never have to be exposed to the user, instead they're used to locate the image associated with a specific
id
ororiginal_filename
in the 'images` table.Of course, if you don't care about giving the original filename to the image when it's displayed, you can just generate a unique filename whenever you want to store it.