# Validation for file

**URL:** <https://www.ruby-forum.com/t/validation-for-file/137129>\
**Category:** Ruby\
**Created:** [May 9, 2008, 6:45am UTC](https://www.ruby-forum.com/t/validation-for-file/137129 "2008-05-09T06:45:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [May 9, 2008, 6:45am UTC](https://www.ruby-forum.com/t/validation-for-file/137129/1 "2008-05-09T06:45:32Z")

</div>

i am developing one application in which user uploaded one csv template  
to my system(file). but we have to take care that user should uploaded  
.csv file only if he trying to upload another file we have to restrict  
it.  
how to check for extention in ruby on rails ya there is another solution  
for it?  
i am doing something like  
file\_name =params[:group\_member\_information].original\_filename  
begin  
f = File.new("#{path}/#{file\_name}", “wb”)  
str = params[:group\_member\_information].read  
f.write str  
f.close

please reply !

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [May 9, 2008, 8:38am UTC](https://www.ruby-forum.com/t/validation-for-file/137129/2 "2008-05-09T08:38:35Z")

</div>

Sunny B. wrote:

> i am developing one application in which user uploaded one csv template  
> to my system(file). but we have to take care that user should uploaded  
> .csv file only if he trying to upload another file we have to restrict  
> it.  
> how to check for extention in ruby on rails

Does this forum say “ruby on rails” anywhere? Or is it your  
understanding that any forum that contains the words “ruby” or “rails”  
could not possibly be related to precious gems or trains.

In the Ruby programming language, which is what this forum is about, you  
could do something like this:

fnames = [“somefile.csv”, “anotherfile.jpg”]

fnames.each do |fname|  
pieces = fname.split(".")

if pieces[-1] != “csv”  
print "bad file: ", fname  
puts  
end

end

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [May 10, 2008, 10:21am UTC](https://www.ruby-forum.com/t/validation-for-file/137129/3 "2008-05-10T10:21:12Z")

</div>

On 09.05.2008 08:38, 7stud – wrote:

> fnames = [“somefile.csv”, “anotherfile.jpg”]
> 
> fnames.each do |fname|  
> pieces = fname.split(".")
> 
> if pieces[-1] != “csv”  
> print "bad file: ", fname  
> puts  
> end
> 
> end

This is a rather weak check as it only tests the file name. Note, that  
you can do this more easily:

ok = /.csv$/i =~ file\_name

Kind regards

robert
