Confused on using basename...

Discussion in 'Perl' started by Sean Berry, Apr 14, 2004.

  1. Sean Berry

    Sean Berry Guest

    I have an online ftp type website for a customer that I wrote in a cgi perl
    script.

    If the person is trying to upload a file that already exists on the server I
    am printing a message telling them to delete the original first.

    My problem is this...
    In order to compare the given file with what is in the database, I need to
    get the name of the file by itself (without the full path). So, if someone
    uploads a file on windows... I should extract file.txt from C:\Documents and
    Settings\All Users\Documents\file.txt

    I can do this using regular expressions, but want to have one command that
    will work on any system rather than doing a different regex based on the
    users os. I tried using:
    $filename = basename($path);
    But this does not seem to be working. Am I missing something, or is there a
    better way to do this.

    TIA for any help.
     
    Sean Berry, Apr 14, 2004
    #1
    1. Advertising

  2. Sean Berry

    Joe Smith Guest

    Sean Berry wrote:

    > I have an online ftp type website for a customer that I wrote in a cgi perl
    > script.
    >
    > If the person is trying to upload a file that already exists on the server I
    > am printing a message telling them to delete the original first.
    >
    > My problem is this...
    > In order to compare the given file with what is in the database, I need to
    > get the name of the file by itself (without the full path). So, if someone
    > uploads a file on windows... I should extract file.txt from C:\Documents and
    > Settings\All Users\Documents\file.txt
    >
    > I can do this using regular expressions, but want to have one command that
    > will work on any system rather than doing a different regex based on the
    > users os. I tried using:
    > $filename = basename($path);
    > But this does not seem to be working. Am I missing something, or is there a
    > better way to do this.


    As you've discovered, basename() is good for breaking down file specifications
    native to the OS that the server is running on. You can't reliably get
    the name of the OS that the client is using since the HTTP headers are
    optional and some HTTP clients lie.

    The string containing the user's name for his/her local file quite often
    contains characters that the server considers to be illegal or undesireable.
    Instead of trying to guess what the client OS uses for directory separator,
    you should be considering just what is allowable on the server.

    If the specification for the server is such that the only characters it
    accepts for file names are upper and lower case alpha, digits 0-9, hyphen,
    underscore and period, then you could use

    my($filename) = $userfile =~ /([-a-zA-Z0-9_.]+)$/;

    to throw away all the unwanted stuff.
    -Joe

    P.S. Next time post to comp.lang.perl.misc - more readers there.
     
    Joe Smith, Apr 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald Fischer
    Replies:
    1
    Views:
    15,250
    Jacob
    Jul 22, 2003
  2. Xah Lee
    Replies:
    14
    Views:
    967
    Michele Dondi
    Jan 27, 2004
  3. Peter Saffrey

    basename with extensions

    Peter Saffrey, Aug 30, 2006, in forum: Python
    Replies:
    1
    Views:
    3,277
    Tal Einat
    Aug 30, 2006
  4. basename in C++

    , Oct 5, 2006, in forum: C++
    Replies:
    4
    Views:
    17,180
    Jens Theisen
    Oct 5, 2006
  5. Thomas Ploch
    Replies:
    2
    Views:
    606
    Thomas Ploch
    Mar 14, 2007
Loading...

Share This Page