# How to read and write files?

**URL:** <https://www.ruby-forum.com/t/how-to-read-and-write-files/158331>\
**Category:** Rails\
**Created:** [January 22, 2009, 5:46am UTC](https://www.ruby-forum.com/t/how-to-read-and-write-files/158331 "2009-01-22T05:46:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [January 22, 2009, 5:46am UTC](https://www.ruby-forum.com/t/how-to-read-and-write-files/158331/1 "2009-01-22T05:46:11Z")

</div>

Dear all,

I have uploaded gif images and stored in folder called banner list. I  
have successfully read and written the uploaded gif files even animated  
gif files. But when i read the same gif file from the the bannerlist  
folder and write in banner folder, i was not able to do so. Please  
suggest on how to read gif images from a folder and write in another  
folder.

I have used code :

if File.exists? RAILS\_ROOT + “/public/images/bannerlist/#{params[:id]}”

a = File.read(“public/images/bannerlist/martin2.gif”)

File.open(“public/images/banner/” + “banner.gif” , “wb”) do |f|

```
  f.write(a)

```

end  
end

For Uploading of images i have used following code and it works fine, so  
please suggest on the above issue of reading and writing of animated gif  
files from one folder to another.

image = params[:image][:blob]

image1=params[:image][:blob].original\_filename

File.open(RAILS\_ROOT + “/public/images/bannerlist/” + image1, “wb”) do  
|f|

f.write(params[:image][:blob].read)

end

please suggest a way for reading and writing of animated gif files from  
one folder to another.

Thanks in advance

Regards,  
Jose

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [January 22, 2009, 5:51am UTC](https://www.ruby-forum.com/t/how-to-read-and-write-files/158331/2 "2009-01-22T05:51:36Z")

</div>

dare ruby wrote:

> I have used code :
> 
> if File.exists? RAILS\_ROOT + “/public/images/bannerlist/#{params[:id]}”

Firstly, start using Pathname. I always put this in environment.rb:

require ‘pathname’  
RailsRoot = Pathname.new(RAILS\_ROOT).expand\_path

That allows this:

if (RailsRoot + ‘public/images/bannerlist’ + params[:id]).exist?

> a = File.read(“public/images/bannerlist/martin2.gif”)

That .read() call neglects the RAILS\_ROOT. Never guess what the current  
folder  
is; always use complete paths.

Next, you are essentially reading a gif into memory and barfing it out  
into a  
file. This is slow, fragile, and high-risk. Consider using FileUtils to  
symlink  
the banner.gif to the rotating banner in the banner list.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [January 22, 2009, 7:40am UTC](https://www.ruby-forum.com/t/how-to-read-and-write-files/158331/3 "2009-01-22T07:40:25Z")

</div>

Why are you reading one file and then writing it to another?

Why don’t you just copy it? [copy (File) - APIdock](http://is.gd/gNve)

And if file operations doesn’t work, open up console and try it there.  
You will see if it was successful, because irb will show true/false.  
If it’s false, then check if your directories have rights to be  
writed.

On 22. Jan, 05:46 h., dare ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [January 22, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/how-to-read-and-write-files/158331/4 "2009-01-22T12:07:18Z")

</div>

> file. This is slow, fragile, and high-risk. Consider using FileUtils to  
> symlink  
> the banner.gif to the rotating banner in the banner list.

Dear Philip thanks for your immediate response, it works fine with  
FileUtils copy method. Thanks for your suggestions.

Regards,  
Jose Martin
