How do I find the current filename, if all I have is the file object?
The file is one that a user uploads, and I want to find its name but, if I use file_object.path, it gives the name that the file was created as, rather than the current name.
That produced a name that seemed like a random bunch of numbers and letters; The sample file was a certificate I created which is the type of file the user will upload, so I want to make sure I show the current file name so it’s not confusing.
The short answer is “you can’t”.
To understand why, in a typical filesystem a file is just a node (or in Unix/Linux-speak, an inode) which can be referred to by many links, and I’m not talking about symlinks.
For example:
In fact, if you look at the Ruby documentation for File::Stat…
http://www.ruby-doc.org/core-1.9.3/File/Stat.html
…you’ll see that there is a method
nlinkthat returns the number of hard links to the file.You’ll also see that there is a method
inspectwhich returns attributes for the file, but no names.