# File management!

**URL:** <https://www.ruby-forum.com/t/file-management/53238>\
**Category:** Rails\
**Created:** [January 30, 2006, 6:34am UTC](https://www.ruby-forum.com/t/file-management/53238 "2006-01-30T06:34:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 30, 2006, 6:34am UTC](https://www.ruby-forum.com/t/file-management/53238/1 "2006-01-30T06:34:12Z")

</div>

Hi, I’m new to ROR. I was wondering how to:

1. Check if the file is being read/downloaded/…
2. Rename files

If you can help or point out any document that I need to read to  
accomplish this, please do. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 30, 2006, 9:37am UTC](https://www.ruby-forum.com/t/file-management/53238/2 "2006-01-30T09:37:58Z")

</div>

vu nguyen wrote:

> Hi, I’m new to ROR. I was wondering how to:
> 
> 1. Check if the file is being read/downloaded/…  
> By (possibly) another process? Via the webserver? You’ll have to add  
> that manually, I think.

> 1. Rename files  
> File.rename(‘oldfilename’, ‘newfilename’)

> If you can help or point out any document that I need to read to  
> accomplish this, please do. Thanks a lot.

[http://www.ruby-doc.org/core/classes/File.html](http://www.ruby-doc.org/core/classes/File.html)

---

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 31, 2006, 6:46am UTC](https://www.ruby-forum.com/t/file-management/53238/3 "2006-01-31T06:46:47Z")

</div>

Wow, thanks a lot. The reason I asked was that I want to allow users  
download files from my host, yet hide the real url from them to prevent  
leaching.

The solution I came up with was: let the user download the file, check  
if the files downloaded (not being read), then rename it. Of course the  
database will keep track of the files for me.  
But today I did some check and found out some thing interesting: even if  
I change the file name while the user downloading it, it wouldnt affect  
their current download → Then I dont have to check if the file is  
being accessed (which is a hard job to do)

Also I found another interesting solution:  
[http://www.webclass.ru/eng/Tutorials/Perl/How\_to\_hide.html](http://www.webclass.ru/eng/Tutorials/Perl/How_to_hide.html)  
Basically instead of sending the file header to the user’s browser, you  
print out the file content. I wonder if it’s possible to do on Rails  
tho, and what happen if the file is pretty big and not an image file…

---

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 31, 2006, 10:12am UTC](https://www.ruby-forum.com/t/file-management/53238/4 "2006-01-31T10:12:23Z")

</div>

vu nguyen wrote:

> being accessed (which is a hard job to do)  
> If the file’s popular, there’s a race condition in there:

Client X sees the page, notes the filename as ‘aaa.zip’.  
Client Y sees the page, notes the filename as ‘aaa.zip’.  
Client X requests the file as ‘aaa.zip’.  
Server renames the file to ‘bbb.zip’.  
Client Y requests the file as ‘aaa.zip’.  
Client Y gets a 404.

Slight snag.

---

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 31, 2006, 11:55am UTC](https://www.ruby-forum.com/t/file-management/53238/5 "2006-01-31T11:55:38Z")

</div>

Huhm, this is mailing list so I cant edit what I posted _\__. Anyway,  
just to add 1 thing: after sending the header to the browser/Dmanager so  
the download can start, I will just rename my file/or the parent  
folder.(which does not affect the current downloads in any way).

---

<div class="post-metadata">

**Author:** ![yellow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/dfb087/32.png) [@yellow](https://www.ruby-forum.com/u/yellow)\
**Post date:** [January 31, 2006, 11:51am UTC](https://www.ruby-forum.com/t/file-management/53238/6 "2006-01-31T11:51:49Z")

</div>

Alex Y. wrote:

> vu nguyen wrote:
> 
> > being accessed (which is a hard job to do)  
> > If the file’s popular, there’s a race condition in there:
> 
> Client X sees the page, notes the filename as ‘aaa.zip’.  
> Client Y sees the page, notes the filename as ‘aaa.zip’.  
> Client X requests the file as ‘aaa.zip’.  
> Server renames the file to ‘bbb.zip’.  
> Client Y requests the file as ‘aaa.zip’.  
> Client Y gets a 404.
> 
> Slight snag.

Interesting, but then you have to rename it back after some time? Or you  
keep track using database?

Talking about link hiding, I used readfile() when I worked with PHP  
(still do). But now I think of a better way: give each file 1 unique id,  
keep track in mysql. Everytime the browser/download manager requests the  
file, I simply send back the header to that file. That way my users can  
use RESUME.  
I can still authenticate and prevent leaching, and my users can resume  
their broken download files.
