# Saving the image

**URL:** <https://www.ruby-forum.com/t/saving-the-image/110427>\
**Category:** Rails\
**Created:** [July 26, 2007, 8:48am UTC](https://www.ruby-forum.com/t/saving-the-image/110427 "2007-07-26T08:48:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xyzt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/839c29/32.png) [@xyzt](https://www.ruby-forum.com/u/xyzt)\
**Post date:** [July 26, 2007, 8:48am UTC](https://www.ruby-forum.com/t/saving-the-image/110427/1 "2007-07-26T08:48:07Z")

</div>

Hello,

I try to prepare a upload page and here’s my controller:

def new  
if request.post?  
begin  
@file = params[:img]  
if @file.original\_filename.length \> 0  
filename = “/images/userimg/” + @file.original\_filename  
File.open(filename, “wb”) { |f| f.write(@file.read) }  
end  
end  
end  
end

I want the file to be saved into the public/images/userimg folder. but i  
get error at runtime: “No such file or directory -  
/images/userimg/arrowd.gif”

what can i do for it?

---

<div class="post-metadata">

**Author:** ![xyzt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/839c29/32.png) [@xyzt](https://www.ruby-forum.com/u/xyzt)\
**Post date:** [July 26, 2007, 9:04am UTC](https://www.ruby-forum.com/t/saving-the-image/110427/2 "2007-07-26T09:04:49Z")

</div>

BahadÄ±r DoÄŸan wrote:

> I want the file to be saved into the public/images/userimg folder. but i  
> get error at runtime: “No such file or directory -  
> /images/userimg/arrowd.gif”
> 
> what can i do for it?

You check if you have folder /images/userimg 🙂

---

<div class="post-metadata">

**Author:** ![xyzt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/839c29/32.png) [@xyzt](https://www.ruby-forum.com/u/xyzt)\
**Post date:** [July 26, 2007, 9:11am UTC](https://www.ruby-forum.com/t/saving-the-image/110427/3 "2007-07-26T09:11:20Z")

</div>

I have it: “public/images/userimg”

Do I need to get the real path of that folder?

Jamal S. wrote:

> BahadÄ±r DoÄŸan wrote:
> 
> > I want the file to be saved into the public/images/userimg folder. but i  
> > get error at runtime: “No such file or directory -  
> > /images/userimg/arrowd.gif”
> > 
> > what can i do for it?
> 
> You check if you have folder /images/userimg 🙂

---

<div class="post-metadata">

**Author:** ![xyzt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/839c29/32.png) [@xyzt](https://www.ruby-forum.com/u/xyzt)\
**Post date:** [July 26, 2007, 9:52am UTC](https://www.ruby-forum.com/t/saving-the-image/110427/4 "2007-07-26T09:52:42Z")

</div>

On Jul 26, 3:11 am, “BahadÄ±r DoÄŸan” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have it: “public/images/userimg”
> 
> Do I need to get the real path of that folder?

Try:  
filename = “#{RAILS\_ROOT}/public/images/userimg/” +  
@file.original\_filename

Be aware that this could be abused. It might be possible for someone  
to put …/ in the filename and do something malicious like add a  
controller to your application or overwrite your log files.

~Rusty

---

<div class="post-metadata">

**Author:** ![xyzt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/839c29/32.png) [@xyzt](https://www.ruby-forum.com/u/xyzt)\
**Post date:** [July 26, 2007, 11:46am UTC](https://www.ruby-forum.com/t/saving-the-image/110427/5 "2007-07-26T11:46:13Z")

</div>

yes,that’s right:  
filename = “#{RAILS\_ROOT}/public/images/userimg/” +
