# Problem realted to upload

**URL:** <https://www.ruby-forum.com/t/problem-realted-to-upload/54701>\
**Category:** Rails\
**Created:** [March 13, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701 "2006-03-13T13:15:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [March 13, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701/1 "2006-03-13T13:15:35Z")

</div>

I want to upload a file.  
And what I am confused about is…should I upload it using a separate  
table or just as another file in the form.  
If as a separate table then I am not able to save it on the system/  
Or/ If not that, then how can i check for the extensions?  
Do reply.  
Thanks.

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [March 13, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701/2 "2006-03-13T19:08:22Z")

</div>

John,  
Have you looked at the file\_column plugin(  
[http://www.kanthak.net/opensource/file\_column/](http://www.kanthak.net/opensource/file_column/))? What do you mean by  
using  
a seperate table? Are you trying to stick the file in the database or  
by  
table do you mean the reference to the file location? As for the file  
extension’s I’m not exactly sure, the ruby File.ftype returns the type  
of  
the file which may be useful for you. Otherwise you might be stuck  
doing  
some regex.

Rob

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [March 14, 2006, 6:45am UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701/3 "2006-03-14T06:45:06Z")

</div>

John,

Here’s a regex that will strip out the extension of a filename. What it  
actually does is returns everything after a ‘.’ is first encountered in  
the  
string, so: “filename.doc” would return “doc”.

file\_name = “my\_file.ext”

regex = Regexp.new(’.(.\*)$’)  
match\_data = regex.match(file\_name)  
file\_extension = match\_data[1]

Hope that helps.

Rob

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [March 14, 2006, 7:52am UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701/4 "2006-03-14T07:52:38Z")

</div>

Rob,  
I tried using validates\_format\_of…  
But didn’t not get the desired results, what i actually did was…

validates\_format\_of :user\_file,  
:with =\> %r{.(doc|pdf)$}i,  
:message =\> “please”  
But gives me

undefined method `user\_file’ for #User:0xb768a07c

If you could help. I am new to Ruby on Rails, so I might be sounding  
silly too but hope you understand and would help.

Thanks.

Rob M. wrote:

> John,
> 
> Here’s a regex that will strip out the extension of a filename. What it  
> actually does is returns everything after a ‘.’ is first encountered in  
> the  
> string, so: “filename.doc” would return “doc”.
> 
> file\_name = “my\_file.ext”
> 
> regex = Regexp.new(’.(.\*)$’)  
> match\_data = regex.match(file\_name)  
> file\_extension = match\_data[1]
> 
> Hope that helps.
> 
> Rob

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [March 14, 2006, 5:59am UTC](https://www.ruby-forum.com/t/problem-realted-to-upload/54701/5 "2006-03-14T05:59:28Z")

</div>

Thankyou Rob.  
I’ll try to be more explanatory.  
Actually, I want to upload a file entered by the user using simple  
“input file” option in the \_form.rhtml code. And after that I want to  
check for the right extensions. That is it needs to just upload the doc  
file.  
Doing this I am stuck up with the extension checking. Though its saving  
everything in the /public/ file very comfortably.  
Hoping to hear more.  
Thanks once again.

Rob M. wrote:

> John,  
> Have you looked at the file\_column plugin(  
> [http://www.kanthak.net/opensource/file\_column/](http://www.kanthak.net/opensource/file_column/))? What do you mean by  
> using  
> a seperate table? Are you trying to stick the file in the database or  
> by  
> table do you mean the reference to the file location? As for the file  
> extension’s I’m not exactly sure, the ruby File.ftype returns the type  
> of  
> the file which may be useful for you. Otherwise you might be stuck  
> doing  
> some regex.
> 
> Rob
