Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Confused on using basename...

Reply
Thread Tools

Confused on using basename...

 
 
Sean Berry
Guest
Posts: n/a
 
      04-14-2004
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.



 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      04-14-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused with error using ofstream rEvolution27@gmail.com C++ 15 03-04-2007 10:21 PM
confused! Dennis J. Tuchler Firefox 4 05-19-2005 12:04 AM
Thunderbird 1.0 got confused with news 'Captain' Kirk DeHaan Firefox 0 12-24-2004 08:16 PM
Still confused...using an HTML Control to modify an HTML tag's value darrel ASP .Net 6 10-15-2004 07:16 PM
Confused and bewildered. DJ Miller ASP .Net 4 09-04-2003 06:10 PM



Advertisments