(rails)如何验证上传的.txt文件是否不是一个图像文件? [英] (rails) how to validate whether an uploaded .txt file is not, say, an image file?

查看:161
本文介绍了(rails)如何验证上传的.txt文件是否不是一个图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传文本文件的字段,我打算将它保存到某个地方,然后将文件的位置存储在数据库中。信息亦读亦读信息范讯信息范范内范范中范范内范范中信息中信息上亦上信息我想这在验证步骤中发生。如何验证这样的事情?另外,你如何获得上传文件的文件名?我总是可以检查是否说'.txt',但为了将来的参考知道如何验证,而不仅仅是文件名将是有益的。

解决方案

方手如果你在一个基于Mac / Linux / Unix的系统上,OSfile命令是标准的,因为它是标准的在文件内部查找标记文件类型的关键字节。 http://en.wikipedia.org/wiki/File_(Unix) I不知道Windows有什么可用,但这可能有帮助:确定Ruby中的文件类型


I have a upload text file field, and with it I plan to save the file somewhere and then store the location of the file in a database. However, I want to make sure the file they uploaded is a .txt file, and not, say, an image file. I imagine this happens in the validation step. How does one validate such a thing? Also, how do you get the filename of the uploaded file? I could always just check if it said '.txt' but for future reference knowing how to validate without just the filename would be helpful.

解决方案

Trying to validate the contents of a file based on the filename extension is opening the door for major hackerdom. It's trivial to change the extension and upload the file.

If you are on a Mac/Linux/Unix-based system the OS "file" command is the standard because it looks inside the file for key bytes that flag file types. http://en.wikipedia.org/wiki/File_(Unix) I'm not sure what's available for Windows, but this might help: Determine file type in Ruby

这篇关于(rails)如何验证上传的.txt文件是否不是一个图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆