I am trying to access the filename of an uploaded file. I have tried to follow the steps and examples of several howtos, but it isn''t working. I might be forgetting some important part, but here is what I have: def testaction @just_filename = @params[''post''][''file''].original_filename @just_filename = File.basename(@just_filename) @just_filename = @just_filename.sub(/[^\w\.\-]/,''_'') end The file uploaded "test file with spaces.jpg" in linux gives out "test_file with spaces.jpg". Notice the underscore only works once. Using Internet Explorer on Windows will give me "C_\...\to\my\test file with spaces.jpg". Notice an underscore only appears once and replaces the colon that would normally appear. Of course, I want the C:\...\path\to\ stuff to disappear, but my sub method isn''t working either. It only gets rid of the first invalid argument and replaces it with an underscore, but neglects to recursively act throughout the entire string. I copied this directly from the howto at http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles but it isn''t working. I would fix the sub method but I have no grasp on regular expressions as I have little PHP experience and am starting RoR. So I have two problems: 1. Wipe out the absolute path given by Internet Explorer, leave only the filename. 2. "Sanitize" my filenames to be inserted into the filesystem and make sure it recursively pursues through the string. I would seriously appreciate any help or code snippets! Thank you for your help in advance! --- [This E-mail scanned for viruses by Declude Virus]
Michael Klein
2005-May-03 00:42 UTC
Re: Easy Regular Expression and File Uploading Questions
Alexey Verkhovsky <alex@...> writes:> > Michael Klein wrote: > > > The file uploaded "test file with spaces.jpg" in linux gives out > > "test_file with spaces.jpg". Notice the underscore only works once. > > String#sub replaces only the first match. String#gsub replaces all > matches. That''s your problem. >Thank you very much, that answers the first question! Now does anyone know an effective way to get rid of Internet Explorer''s original_filename?
Alexey Verkhovsky
2005-May-03 00:45 UTC
Re: Easy Regular Expression and File Uploading Questions
Michael Klein wrote:> The file uploaded "test file with spaces.jpg" in linux gives out > "test_file with spaces.jpg". Notice the underscore only works once.String#sub replaces only the first match. String#gsub replaces all matches. That''s your problem. -- Best regards, Alexey Verkhovsky Ruby Forum: http://ruby-forum.org (moderator) RForum: http://rforum.andreas-s.net (co-author) Instiki: http://instiki.org (maintainer)
Gavin Sinclair
2005-May-03 01:09 UTC
Re: Re: Easy Regular Expression and File Uploading Questions
On Tuesday, May 3, 2005, 10:42:46 AM, Michael wrote:> Alexey Verkhovsky <alex@...> writes:>> >> Michael Klein wrote: >> >> > The file uploaded "test file with spaces.jpg" in linux gives out >> > "test_file with spaces.jpg". Notice the underscore only works once. >> >> String#sub replaces only the first match. String#gsub replaces all >> matches. That''s your problem. >>> Thank you very much, that answers the first question!> Now does anyone know an effective way to get rid of Internet Explorer''s > original_filename?I found your original post hard to read, but I suspect you may want File.basename(''C:\blah\blah\blah.txt'') -> ''blah.txt'' Gavin
Michael Klein
2005-May-03 02:13 UTC
Re: Easy Regular Expression and File Uploading Questions
Gavin Sinclair <gsinclair@...> writes:> > I found your original post hard to read, but I suspect you may want > > File.basename(''C:\blah\blah\blah.txt'') -> ''blah.txt'' > > Gavin >Gavin, that''s exactly it. sorry about being unclear. Michael