Confused on using basename...

S

Sean Berry

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

Joe Smith

Sean said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top