# About the upload file validate

**URL:** <https://www.ruby-forum.com/t/about-the-upload-file-validate/142787>\
**Category:** Rails\
**Created:** [July 11, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/about-the-upload-file-validate/142787 "2008-07-11T14:47:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 11, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/about-the-upload-file-validate/142787/1 "2008-07-11T14:47:01Z")

</div>

hello:  
I have a method like following:  
def file=(file\_field)  
#remember to validate the content type  
File.open("#{RAILS\_ROOT}/public/images/users/#  
{file\_field.original\_filename}",“wb+”) do|i|  
i.write(file\_field.read)  
end  
self.photo="/images/users/"+file\_field.original\_filename  
end

i want to use it to upload the image file locally,but how do i make  
sure that the uploaded file is image file?  
if use the validate\_format\_of method,i wonder its ability,because any  
file can pass its validate as long as the file is named like a  
image,such as .jpg.  
or use the file\_field.content\_type to manage the validate?If this  
way,how to write the code?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 11, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/about-the-upload-file-validate/142787/2 "2008-07-11T15:54:35Z")

</div>

None of these mimetype based methods do anything else but check the  
file extension as far as I know.

As a first step we would trust our customers not to rename textfiles  
and  
upload them as images. If somebody should do it more than once, we  
would  
most likely lock his account. But we never had any trouble of that  
kind.

If you must make sure, that you have an real image file, maybe you  
can use a tool like RMagick, open it and try to access some of the  
internal binary data like size, to see if RMagick gives an error.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 11, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/about-the-upload-file-validate/142787/3 "2008-07-11T17:15:14Z")

</div>

Thorsten M. wrote:

> None of these mimetype based methods do anything else but check the  
> file extension as far as I know.
> 
> As a first step we would trust our customers not to rename textfiles  
> and  
> upload them as images. If somebody should do it more than once, we  
> would  
> most likely lock his account. But we never had any trouble of that  
> kind.
> 
> If you must make sure, that you have an real image file, maybe you  
> can use a tool like RMagick, open it and try to access some of the  
> internal binary data like size, to see if RMagick gives an error.

Thank you!I had thought there was a easy and well\_known way to do it.
