Solved

Linux vs. Windows file upload names

Posted on 2002-03-25
7
175 Views
Last Modified: 2013-12-25
Greetings experts,

Not sure of the best place to ask this question, but here we go. I have a simple CGI form that allows the user to upload a file, with the basic <input type="file" ... >.

It works fine, I'm able to get the file on my Linux Apache server, no problem.

But here's the problem: If the client was running on a Linux box, the filename arrives in my CGI script as just the base file name:

"/hello/new/world" arrives as just "world". This is fine because that's all I want, the basename.

BUT, if the client was running on a Windows box it arrives as the fully qualified pathname:

"C:\hello\new\world"

This would be OK if I could easily split the path to get the basename, which I could using "\".  The problem is that "\n" is interpreted as a newline character on the receiving end in my CGI script, which is written in Python.

Certainly, regardless of the CGI language, someone has crossed this bridge before?!

Any thoughts would be appreciated.

Thanks
- Nelson
0
Comment
Question by:nchenkin
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 250 total points
ID: 6894546
nchenkin,

".. which I could using "\".  The  problem is that "\n" is interpreted as a newline character on the receiving end in my CGI script, which  is written in Python...."

From a CGI point of view here is an alternative that you might want to consider.

use / as the directory delimiter.

So in the case of a Windows based client, let say you get C:\hello\new\world you replace all \'s with /'s and then process them.

Using this approach i can see 2 advantages.

1) Regardless of which OS the client is using, you are standardizing on the / as the delimiter.
This will make your code work with any client OS
2) You dont run into the \n problem as the search-n-replace has replaced \ with/

Hope that helps.
0
 

Author Comment

by:nchenkin
ID: 6895022
maneshr,

Thanks for the comment. The reason I didn't do this is because I *thought* that the "\n" had already been escaped and arrived at my CGI as a newline. I think this was in error. After your comment I checked it and it looks like  the "\" is still there intact.

Let me do some more checks and then I'll give you the points.

- NC
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6895374
nchenkin,

".. After your comment I checked  it and it looks like  the "\" is still there intact...."

Aha!! Good catch!!!

"..Let me do some more checks..."

Hopefully you will have a working solution with you soon.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:Peewee
ID: 6896049
nchenkin,

here's what i'm thinking:

get rid of a new line if it exists with a chomp.
then split on a slash which needs to be escaped.

this i think should do the trick..
regards
Peewee

<input type="file" ... >.


chomp $file;
my @array = split(/\\/,$file);

foreach (@array)
{
     print "array:\t$_\n";
}
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6896060
nchenkin,

also i'm quite sure this module will be of help to you:

regards Peewee
############


 use File::Basename;
 
           ($name,$path,$suffix) = fileparse($fullname,@suffixlist)
           fileparse_set_fstype($os_string);
           $basename = basename($fullname,@suffixlist);
           $dirname = dirname($fullname);
 
           ($name,$path,$suffix) = fileparse("lib/File/Basename.pm","\.pm");
           fileparse_set_fstype("VMS");
           $basename = basename("lib/File/Basename.pm",".pm");
           $dirname = dirname("lib/File/Basename.pm");
0
 

Author Comment

by:nchenkin
ID: 6896705
Thanks maneshr,

Things are working now. This actually was what I initially was going to do and didn't really need to ask the question! Your comment made me go back and verify that my first impressions (due to some Python string operations I attempted) were incorrect. So, I am now simply converting all the '\' to '/'.

Pewee,
Thanks for your info as well.

Regards,
Nelson
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6897102
nchenkin,

"..Thanks maneshr,..."

You're welcome.

"..Things are working now..."

Glad to know you got the solution you were looking for.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now