# Permissions on uploaded files, TinyFile

**URL:** <https://www.ruby-forum.com/t/permissions-on-uploaded-files-tinyfile/62845>\
**Category:** Rails\
**Created:** [June 8, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/permissions-on-uploaded-files-tinyfile/62845 "2006-06-08T18:25:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [June 8, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/permissions-on-uploaded-files-tinyfile/62845/1 "2006-06-08T18:25:14Z")

</div>

Hi, im getting a little trouble with this, im using TinyFile as the  
example to upload files  
[http://wiki.rubyonrails.com/rails/pages/TinyFile/versions/20](http://wiki.rubyonrails.com/rails/pages/TinyFile/versions/20)

All goes well but the files uploaded are saved with permissions 600  
(linux) and in the case of images arent showed on the browser(but  
uploaded and stored in the server without problems) i have to manually  
ftp to the uploaded files folder and change it to 644 to the images  
shows on the browser.

I dont know if its possible to define the permissions of the file that  
will keep when saved in the model or the controller, or maybe its a  
server issue.

Hope someone can help with this, small problem but not so easy to me to  
fix it.

---

<div class="post-metadata">

**Author:** ![anabia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/35a633/32.png) [@anabia](https://www.ruby-forum.com/u/anabia)\
**Post date:** [June 8, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/permissions-on-uploaded-files-tinyfile/62845/2 "2006-06-08T23:34:43Z")

</div>

Anyone? i have been trying to set the permissions of the uploaded file  
with: FileUtils.chmod 0644, %w(File) , on the before\_create action but  
doesnt seems to work, still the files are saved with 600, her is the  
code im using:

#view-----------------

\<%= start\_form\_tag( {:action =\> ‘create’ }, :multipart =\> true )%\>  
\<%= render\_partial ‘form’ %\>  
  
  
  
  
\<%= submit\_tag “Subir imagen” %\>  
\<%= end\_form\_tag %\>

#controller-----------------------

class ResourcesController \< AdController  
layout ‘admin’  
before\_filter :index  
def index  
@resources = Resource.find(:all, :order =\> “filename ASC”)  
#@resource\_pages, @resources = paginate :resource, :per\_page =\> 200  
end

def download  
send\_file Resource.find(params[:id]).path\_to\_file, :stream =\> true  
end

def create  
@resource = Resource.new(params[:resource])

```
return render :action => 'new' unless request.post? and 

```

@resource.save  
flash[:notice] = “sucess: **#{@resource.filename}**.”  
redirect\_to :action =\> ‘index’  
end

def rename

# @resource = Resource.find(params[:id])

# return render unless

Resource.find(params[:id]).rename(params[:resource][:filename]) rescue  
return  
return render unless  
(@resource=Resource.find(params[:id])).rename(params[:resource][:filename])  
rescue return  
flash[:notice] = “success.”

```
redirect_to :action => 'index'

```

end

def destroy  
Resource.find(params[:id]).destroy  
flash[:notice] = “deleted.”  
redirect\_to :action =\> ‘index’  
end  
end

#model----------------------------

class Resource \< ActiveRecord::Base

validates\_presence\_of :filename  
validates\_uniqueness\_of :filename, :message =\> “already exists, try  
uploading another file or deleting first.”

def rename(new\_name, old\_path=self.path\_to\_file)  
File.rename old\_path, self.path\_to\_file(new\_name) if  
self.update\_attributes( :filename =\> new\_name )  
end

def before\_destroy

# delete the file from the filesystem

File.delete self.path\_to\_file  
end

def file=(uploaded\_file, i=1)  
@uploaded\_file = uploaded\_file  
fn=self.filename=sanitize\_filename(@uploaded\_file.original\_filename)  
self.filename=fn+(i+=1).to\_s while File.exists? (self.path\_to\_file)

#tmpfile = filename.sub(/^(.\*?)(.[^.]+)?$/,’\1’+"#{i+=1}"+’\2’)  
while File.exists? (self.path\_to\_file)  
end

def before\_create

# if it’s large enough to be a real file

return FileUtils.copy( @uploaded\_file.local\_path, self.path\_to\_file)  
if @uploaded\_file.instance\_of?(Tempfile)

# else

File.open(self.path\_to\_file, “w”) { |f| f.write(@uploaded\_file.read) }  
FileUtils.chmod 0644, %w(File)  
end

# setup file location

def path\_to\_file ( file = self.filename)  
RAILS\_ROOT + “/public/images/publicas/” + file  
end

def sanitize\_filename(name)

# get only the filename, not the whole path and

# replace all none alphanumeric, underscore or periods with underscore

File.basename(name.gsub(’\’, ‘/’)).gsub(/[^\w.-]/,’\_’)  
end

end

As i said, the files are uploaded and stored without problems but the  
600 permissions that later i have to manually change it by ftp to 644.

Hop someone can help ☹
