module FilenameSanitizer def sanitize_filename(filename) returning filename.strip do |name| # Get only the filename, not the whole path name.gsub! /^.*(\\|\/)/, '' # Replace all non alphanumeric, underscore or periods with underscore name.gsub! /[^\w\.\-]/, '_' end end end