Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Extracting filename from a path string

by Snaps_Provolone (Novice)
on Apr 20, 2002 at 12:58 UTC ( #160772=perlquestion: print w/replies, xml ) Need Help??

Snaps_Provolone has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to work with CGI.pm to support file uploads. When the form is submited the filename includes the path from the client. I need to strip this extra path off the filename before I can open and write the file locally. I have tried a few things and the results were odd. Any ideas?
$path = "\path1\path2\path3\file.ext"; $path =~ s/(.*)\\(.*)/$2/gi; print "$path";
Result = "path1path2path3 ile.ext" Note: The wierd character after 3 looks like a little circle on top of a cross in my command prompt window on NT.
use File::Spec; $path = "\path1\path2\path3\file.ext"; ($volume,$directories,$file) = File::Spec->splitpath($path); print "$file";
Result = "path1path2path3 ile.ext" Note: Again there is the little funky character. Now for the kicker -
$path = "`path1`path2`path3`file.ext"; $path =~ s/(.*)`(.*)/$2/gi; print "$path";
Result - "file.ext" Now thats what I am after hehe. I don't know why but for some reason the \ character seems to be causing me some troubles. I even tried using \\ and \x5C I don't know what else to try. I have read everything I can find on this suject, and have tried everything, but all seem to fail with this path.

Replies are listed 'Best First'.
Re: Extracting filename from a path string
by rob_au (Abbot) on Apr 20, 2002 at 13:33 UTC
    The way which I handled this within CGI::Upload was to make use of File::Basename and HTTP::BrowserDetect. For example:

    use File::Basename; use HTTP::BrowserDetect; fileparse_set_fstype( do { my $browser = HTTP::BrowserDetect->new; return 'MSWin32' if $browser->windows; return 'MacOS' if $browser->mac; $^O; } ); my @file = basename($path, '\.[^\.]*');

    This code makes use of HTTP::BrowserDetect to determine the browser and thus operating system which has submitted the multipart-encoded request. This is then used to set the file-system parsing routines of File::Basename appropriately - The basename method of this module is then used to strip the directory path and return the submitted file name.

     

Re: Extracting filename from a path string
by perlplexer (Hermit) on Apr 20, 2002 at 13:05 UTC
    $path = "\path1\path2\path3\file.ext"; # change " to ' and # you'll be fine $path = '\path1\path2\path3\file.ext';
    If you escape a character with \ in a double-quoted string it'll have a different meaning.
    For example, the well known sequence "\r\n" actually translates to CR LF instead of \r\n.

    --perlplexer
      Boy I feel dumb now! LoL Thanks guys!
Re: Extracting filename from a path string
by dmmiller2k (Chaplain) on Apr 20, 2002 at 13:24 UTC

    What about the venerable, File::Spec?

    Or, as a teaching exercise go right ahead and reinvent the wheel...

    dmm

    GIVE a man a fish you feed him for a day
    TEACH him to fish and you feed him for a lifetime
(smitz)Re: Extracting filename from a path string
by smitz (Chaplain) on Apr 20, 2002 at 13:08 UTC
    This is tried and tested (but not v. elegant) on WinX machines:

    if ($filepath =~ /([^\/\\]+)$/) { $file = $1; } else { $file = $filepath; }
    where $filepath = 'c:\windows\foo\bar\quux\baz.txt'; Hope that helps,
    SMiTZ
Re: Extracting filename from a path string
by beebware (Pilgrim) on Apr 21, 2002 at 12:24 UTC
    Question: Do you really really want to trust the file name submitted by the user/browser? Or would you prefer to use the temp file name provided by CGI.pm or one generated yourself which you'll know doesn't contain any dodgy characters (top-bit set, spaces, 'rm -rf' type things). If you are just displaying the filename, I see no problem (but then why not display the path as well?), but the thought of actually using it as a filename on your server brings me out in a shiver!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://160772]
Approved by smitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2023-12-02 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (18 votes). Check out past polls.

    Notices?