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

Re: file name parsing to get the original file name

by arthas (Hermit)
on Aug 19, 2003 at 13:24 UTC ( [id://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:

fileparse_set_fstype("MSWin32");

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).

Michele.

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...
      c:\path\file.txt
      file.txt
      .
      c:/path
      
      ...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 :
    fileparse_set_fstype("MSWin32");
    Because my html shows an error when I add that line.

    Thanks

      Hi all,

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

      Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-03-29 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found