# How to upload only jpeg & gif & png images into public/image

**URL:** <https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572>\
**Category:** Rails\
**Created:** [May 4, 2006, 9:28am UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572 "2006-05-04T09:28:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 4, 2006, 9:28am UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/1 "2006-05-04T09:28:04Z")

</div>

Hello iam new to rubyonrails. Please any one help me out in “How to  
upload  
only jpeg & gif & png images into public/images using rubyonrails”

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 4, 2006, 10:04am UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/2 "2006-05-04T10:04:32Z")

</div>

Already I go thru this link, I implemented in my application. The images  
are  
uploaded but those are not as the original images. Overlapped with some  
colours. But one image is uploaded successfully its size is 977Bytes.  
If it is 2, or 1.4 KB or more than this not uploaded as original images

so plz help me out in this thing

---

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 4, 2006, 9:40am UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/3 "2006-05-04T09:40:11Z")

</div>

hi there,  
I hate when people send me an URL instead of an answer but I think you  
should read this one: 🙂  
[http://wiki.rubyonrails.com/rails/pages/HowtoUploadFiles](http://wiki.rubyonrails.com/rails/pages/HowtoUploadFiles)  
to check the file type the example talks about  
person[‘picture’].content\_type  
I’ve never tried it BTW,  
HTH!  
Enrico

2006/5/4, Surekha M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 

–  
“The only thing necessary for the triumph of evil  
is for good men to do nothing”  
Edmund Burke

---

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 10, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/4 "2006-05-10T23:53:30Z")

</div>

Surekha M. wrote:

> Already I go thru this link, I implemented in my application. The images  
> are  
> uploaded but those are not as the original images. Overlapped with some  
> colours. But one image is uploaded successfully its size is 977Bytes.  
> If it is 2, or 1.4 KB or more than this not uploaded as original images
> 
> so plz help me out in this thing

Make sure that in your database table you have the data column set to  
‘longblob’ not just blob. it will crop the image if you don’t!

jon

---

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 12, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/5 "2006-05-12T14:47:15Z")

</div>

Well,

```
 It looks like no one intends to help you, which is pretty much the 

```

experience that Ive had on this mailing list. So heres some of my code.  
This is in my pcomic\_admin controller that allows editors for my  
magazine to submit comics. The code is not finished yet. It doesnt  
check for errors in alot of places that it should. It also does not  
check the file size. Im also wary of allowing uploads to the public  
directory. Im thinking that the image should actually go into a sub  
folder with black hole permissions, similar to an ftp setup. Maybe  
others on this list can give an opinion, that would be nice for once.

Dorian

def create  
if @session[‘user’].nil?  
redirect\_to :action =\> ‘login’  
elsif params[:comicpic].content\_type.chomp == “image/jpeg”  
@user = User.find(@session[‘user’].id)  
File.open("…/public/comics/#{@user.id}/#{params[:comicpic].original\_filename}",  
“w”) do  
|f| f.write(params[:comicpic].read)  
end  
@comic = Pendingcomic.new(params[:comic])  
@comic[‘image\_url’] =  
“/comics/#{@user.id}/#{params[:comicpic].original\_filename}”  
@user.pendingcomics.push(@comic)  
redirect\_to :action =\> ‘list’  
else  
redirect\_to :action =\> ‘list’  
end  
end

Surekha M. wrote:

> Hello iam new to rubyonrails. Please any one help me out in “How to  
> upload  
> only jpeg & gif & png images into public/images using rubyonrails”
> 
> Thanks in Advance

---

<div class="post-metadata">

**Author:** ![surekha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@surekha](https://www.ruby-forum.com/u/surekha)\
**Post date:** [May 12, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/how-to-upload-only-jpeg-gif-png-images-into-public-image/59572/6 "2006-05-12T15:15:13Z")

</div>

You could also use file\_column plugin:  
[http://www.kanthak.net/opensource/file\_column/](http://www.kanthak.net/opensource/file_column/)

You can install the latest revision like this:  
script/plugin install  
[http://opensvn.csie.org/rails\_file\_column/plugins/file\_column/trunk](http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk)

Then if you want you can generate docs for this plugin:  
rake doc:plugins

Now if you want to allow uploading of specific formats you can do:

class Entry \< ActiveRecord::Base  
file\_column :image  
validates\_file\_format\_of :field, :in =\> [“gif”, “png”, “jpg”]  
end

And that’s all. Really simple.
