Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: file name parsing to get the original file name

by arthas (Hermit)
on Aug 19, 2003 at 13:24 UTC ( #284885=note: print w/replies, xml ) Need Help??

in reply to file name parsing to get the original file name

As Abigail-II suggested, use File::BaseName. However, if you are parsing HTML form input, beware: the module is not going to work regardless of the operating system of the client. Windows machines put backslashes (\) instead of slashes (/) for instance, so if you need to receive file uploaded by a Windows client and your program is on an Unix server, you'll need to do:


before calling the parsing function. The default FS type of the module depends on the operaing system where the script runs (it's decided upon examining the $^O variabile).


Replies are listed 'Best First'.
Re: Re: file name parsing to get the original file name
by halley (Prior) on Aug 19, 2003 at 13:46 UTC
    My general rule of thumb is to use forward slashes wherever you can get away with it, and use OS-specific separators only when testing shows that they're required. Be ready to accept the OS-specific separators or the forward slashes whenever the user supplies them.

    I just tried this test on Windows with Perl 5.6.0. It shows that File::Basename accepts forward or backslashes on Windows. This is not a thorough test on all platforms and versions.

    use File::Basename qw(basename dirname); print basename("c:\\path\\file.txt"), $/; print basename("c:/path/file.txt"), $/; print dirname("c:\\path\\file.txt"), $/; print dirname("c:/path/file.txt"), $/; __OUTPUT__ file.txt file.txt c:\path c:/path

    "Be lenient in what you accept, strict in what you produce."

    [ e d @ h a l l e y . c c ]

      Output on a Linux box is...
      ...which is obviously wrong if you need to deal with the win32 input.
Re: Re: file name parsing to get the original file name
by tja_ariani (Acolyte) on Aug 20, 2003 at 07:10 UTC
    Hi Michele,

    I was trying your solution since my parsing is indeed HTML form input.Tried the other solutions but can't work, it still does not recognize the '\'

    Is there anything that I should need to add before inserting the :
    Because my html shows an error when I add that line.


      Hi all,

      Thanks for the help, for my post just now I realized that the use File::Basename shouldn't use the qw(...)


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284885]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-08-15 05:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found