如何验证上传的文件是否是图片? [通过 HTML5 的文件 API 发送的文件,通过 php://input 接收]
我正在使用 HTML5 的文件 API (教程此处),我需要验证文件是否是图像。主要问题是,通过正常上传,我可以调用 getimagesize() 并将临时文件的路径作为参数,然后决定是否将其存储在服务器上。
但是,使用文件上传 API,可以通过 php://input 接收文件
,它返回传输的实际位。
在这种情况下验证的最佳方法是什么?我是否必须将这些位存储到物理位置,然后验证它并随后删除它,或者这是更好的方法吗?
谢谢。
I'm working with the HTML5's File API (tutorial here) and I need to validate whether a file is an image or not. The main issue is that with a normal upload, I could call getimagesize()
and as the parameter, the path to the temporary file -- and then decide to store it on the server or not.
However, with the File Upload API, the files are received with php://input
, which returns the actual bits of the transfer.
What's the best way to validate in this situation? Do I have to store the bits to a physical location and then validate it and delete it afterwards or is it a better way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是图像,您会将其存储在磁盘上吗?如果是,那么我认为最好的方法是创建一个临时文件,写入数据,然后检查它是否是具有常规功能的图像,如果是则将临时文件移动到最终目的地,否则就删除它。
如果由于某种原因您不想这样做,您可以检查幻数 在传入数据中(这里是更完整的列表)。但请记住,这比通常的解决方案更有可能失败。
If it's an image will you store it on disk? If yes, then I'd say the best way would be to create a temporary file, write the data and then check if it's an image with the regular functions, if it is move the temporary file to the final destination, otherwise just delete it.
If for some reason you don't want to do this you can check for magic numbers in the incoming data (here is a more complete list). However bare in mind that this is more likely to fail than the usual solutions.