I have a form with which one can upload images. However, Internet Explorer for PC is a very clever program, and instead of putting the filename in the 'filename' attribute of the request object, it puts the whole local path of the file. This of course is unsuitable for a filename on the server. However, due the the escape nature of the backslash, I'm having a tremendous problem writing a function to clean up this mess. I am certain I used to have a code snippit to do this, but cannot find it. Can anyone give me a hand? --- Edward J. Pollard University of Lethbridge Web Development
On Friday, September 19, 2003, at 04:03 PM, Edward Pollard wrote:
Can anyone give me a hand?
It's *always* right when you give up, when you ask people for a hand, that Google returns what you need. I think it's a "Make idiots look foolish" feature. They should turn that off. For the sake of completeness: filename[max(string.rfind(filename, '/'),string.rfind(filename, '\\'),string.rfind(filename, ':'),)+1:] Apologies for crowding your inboxes. --- Edward J. Pollard University of Lethbridge Web Development
On Friday 19 September 2003 05:16 pm, Edward Pollard wrote:
On Friday, September 19, 2003, at 04:03 PM, Edward Pollard wrote:
Can anyone give me a hand?
It's *always* right when you give up, when you ask people for a hand, that Google returns what you need. I think it's a "Make idiots look foolish" feature. They should turn that off.
For the sake of completeness: filename[max(string.rfind(filename, '/'),string.rfind(filename, '\\'),string.rfind(filename, ':'),)+1:]
Sheesh, wouldn't str(filename).split('\\')[-1] be easier? Or does that miss some case? Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
I wanted to revisit this based on my experiences. The problem was cleaning filenames from file uploads in browers that pass the entire local path to the server. Stupid browsers. os.path.basename seems to only work in the context of the path as given on the local system: ie. when used on Unix, it does not fix up Windows filenames. At the very least I can't get it to work on my Mac OS X box. Terry's suggestion again neglects non-Windows paths where forward slash or semicolon can be the separator. I'm left thinking my algorithm - find the last slash, backslash, or semicolon, and go to the end - is the most universal approach. Although I'm entirely reluctant to come to the feet of the masters and contradict them ;-) Ed On Friday, September 19, 2003, at 06:33 PM, Terry Hancock wrote:
On Friday 19 September 2003 05:16 pm, Edward Pollard wrote:
For the sake of completeness: filename[max(string.rfind(filename, '/'),string.rfind(filename, '\\'),string.rfind(filename, ':'),)+1:]
str(filename).split('\\')[-1]
On Friday, September 19, 2003, at 04:44 PM, Paul Winkler wrote:
Or you could let the python core developers do it for you:
os.path.basename(foo)
The problem was cleaning filenames from file uploads in browers that pass the entire local path to the server. Stupid browsers.
os.path.basename seems to only work in the context of the path as given on the local system: ie. when used on Unix, it does not fix up Windows filenames. At the very least I can't get it to work on my Mac OS X box.
Terry's suggestion again neglects non-Windows paths where forward slash or semicolon can be the separator.
I'm left thinking my algorithm - find the last slash, backslash, or semicolon, and go to the end - is the most universal approach.
Probably. There are catches: a colon can be part of a valid filename in Unix. What else beside IE is broken in this particular stupid way? (I've not heard of other browsers doing this.) If it's just IE you could just correct for Windows paths. If IE Mac is included, well, you can't break on colons unless you're sure it's not a Mac path. Unless it translates it into a Windows path, which wouldn't surprise me in the least. If you really want full coverage, I think you'll have to figure out what a string is (Mac path, Unix path, Win path, just a filename) and then parse it using platform rules. How exactly to do this I can't say... --jcc -- "My point and period will be throughly wrought, Or well or ill, as this day's battle's fought."
-----Original Message----- From: zope-bounces@zope.org [mailto:zope-bounces@zope.org]On Behalf Of J. Cameron Cooper Sent: martedì 30 settembre 2003 3.52 To: Edward Pollard; zope@zope.org Subject: Re: [Zope] Cleaning Filenames from Windows
Maybe this is not a catch-all solution, but it works for me. target_fname=target_fname[max(string.rfind(target_fname,'/'), string.rfind(target_fname,'\\'), string.rfind(target_fname,':') )+1:] hope this helps, __peppo
On Fri, 2003-09-19 at 15:03, Edward Pollard wrote:
I have a form with which one can upload images.
However, Internet Explorer for PC is a very clever program, and instead of putting the filename in the 'filename' attribute of the request object, it puts the whole local path of the file. This of course is unsuitable for a filename on the server.
And for any remaining IE6 users who may happen to value their privacy.
However, due the the escape nature of the backslash, I'm having a tremendous problem writing a function to clean up this mess. I am certain I used to have a code snippit to do this, but cannot find it.
I assume you want a regex that captures everything after the final backslash? How about (untested): '.*\\([\w|\d]+)' HTH, Dylan
On Fri, Sep 19, 2003 at 03:35:40PM -0700, Dylan Reinhardt wrote:
On Fri, 2003-09-19 at 15:03, Edward Pollard wrote:
I have a form with which one can upload images.
However, Internet Explorer for PC is a very clever program, and instead of putting the filename in the 'filename' attribute of the request object, it puts the whole local path of the file. This of course is unsuitable for a filename on the server.
And for any remaining IE6 users who may happen to value their privacy.
However, due the the escape nature of the backslash, I'm having a tremendous problem writing a function to clean up this mess. I am certain I used to have a code snippit to do this, but cannot find it.
I assume you want a regex that captures everything after the final backslash?
How about (untested):
'.*\\([\w|\d]+)'
Or you could let the python core developers do it for you: os.path.basename(foo) :-) of course, you can't import that in a Script (Python) so it'll have to be done in an external method or Product code. But then, the same is true of re. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's SUPER MAN MADE OF BUTTER! (random hero from isometric.spaceninja.com)
On Fri, 2003-09-19 at 15:44, Paul Winkler wrote:
On Fri, Sep 19, 2003 at 03:35:40PM -0700, Dylan Reinhardt wrote:
How about (untested):
'.*\\([\w|\d]+)'
Or you could let the python core developers do it for you:
os.path.basename(foo)
Ah... nice catch. Seems like every time I try to do something clever, I find out the Python developers have already done it better. :-) Dylan
participants (6)
-
Dylan Reinhardt -
Edward Pollard -
Giuseppe Bonelli -
J. Cameron Cooper -
Paul Winkler -
Terry Hancock