homepage Welcome to WebmasterWorld Guest from 50.17.27.205
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Using PERL to create a file upload script
but it save with original file name
donahuej




msg:438330
 2:00 pm on Dec 18, 2004 (gmt 0)

I basically copied the upload script out of the CGI.pm book, but in the example it just saved statically to "saved_file.out" and I want to have it remotely save with the original file name. Using pattern matching I can strip the full path down to just the file name, but it wont save to it!
Also, when I change the $filename variable to anything else, it saves fine. There's just something askew with the variable after the pattern matching.

sub upload_file {
my ($data, $filename);
my $file = param( 'upload' );
my $type = uploadInfo($file)->{'Content-Type'};

if (!$file) {
print "No file uploaded!";
return;
}
($filename = $file) =~ s /^\w.+\\//ig;
print $filename, br;
print $file,br,$type,br;
open (SAVE,">./$filename") die $!;
while (read($file,$data,1024)) {
print SAVE $data;
}
close SAVE;
}

Any ideas?
peace,
~Joe Donahue

 

adni18




msg:438331
 2:18 pm on Dec 18, 2004 (gmt 0)

<$file>

should return the file on the user's computer. the open() function is for opening files on the server.

adni18




msg:438332
 2:21 pm on Dec 18, 2004 (gmt 0)

Oops, strike that. I thought you were using it to open the file off the user's computer. So, are you trying to open the file off your server with the same filename as the one the user uploaded?

donahuej




msg:438333
 2:27 pm on Dec 18, 2004 (gmt 0)

I guess I should have been a little more specific. It is an upload script, so the OPEN actually creates the file on the server, and the file is read from the local source and then saved to the server, 1024 bytes at a time. The trouble lies in naming the file that is created on the server.
peace,
~Joe Donahue

[edited by: donahuej at 2:44 pm (utc) on Dec. 18, 2004]

donahuej




msg:438334
 2:36 pm on Dec 18, 2004 (gmt 0)

[edited for content] Sorry!
peace,
Joe Donahue

[edited by: donahuej at 2:43 pm (utc) on Dec. 18, 2004]

The Contractor




msg:438335
 2:39 pm on Dec 18, 2004 (gmt 0)

Posting URL's is against the TOS of this forum ;)

donahuej




msg:438336
 2:45 pm on Dec 18, 2004 (gmt 0)

Thanks for the tip! Any ideas on the file upload script though? :o) j/k, I appreciate your help in my forum etiquette.

adni18




msg:438337
 3:15 pm on Dec 18, 2004 (gmt 0)

So, where does the script save the file?

donahuej




msg:438338
 4:07 pm on Dec 18, 2004 (gmt 0)

When I force a name ($filename = "test.jpg") it saves it just fine to the cgi-bin directory, but in its current state it doesnt save it anywhere, and actually terminates the script. I added a link to return to the original page after the block, and the script never gets to it when I use a filename that is pulled from $file.

So weird....

adni18




msg:438339
 4:44 pm on Dec 18, 2004 (gmt 0)

Are you using use CGI;?


my $file = param( 'upload' );

should be

$q=new CGI;
$file = $q -> param( 'upload' );

donahuej




msg:438340
 4:47 pm on Dec 18, 2004 (gmt 0)

I am using use CGI, however I am not using the object-oriented syntax you posted. I have used that before, but this script is simply the function-oriented syntax. Thanks though!

peace,
~Joe Donahue

adni18




msg:438341
 4:47 pm on Dec 18, 2004 (gmt 0)

also, if you are using CGI, the "my" prefixes should be eliminated.

donahuej




msg:438342
 4:49 pm on Dec 18, 2004 (gmt 0)

the "my" prefixes are part of Use Strict, and otherwise work fine (and are encouraged to be used) in scripts.

At least, thats my understanding...

adni18




msg:438343
 4:54 pm on Dec 18, 2004 (gmt 0)

in my experience, use strict and use CGI don't go together.

adni18




msg:438344
 4:57 pm on Dec 18, 2004 (gmt 0)

I believe that the problem in the filtering of the filename resides in:

my $type = uploadInfo($file)->{'Content-Type'};
/quote]

If you remove it, the filter works. :-D

donahuej




msg:438345
 4:59 pm on Dec 18, 2004 (gmt 0)

that line just finds the MIME type of the file...

I tried it without that line and still had no success. Honestly, thanks for all your help in this so far though.

The Contractor




msg:438346
 5:16 pm on Dec 18, 2004 (gmt 0)

I sticked a url to you

donahuej




msg:438347
 5:26 pm on Dec 18, 2004 (gmt 0)

Yeah, and I got it.. bit it looks big and scary, and bigger than what I need! My nice little script works great except for this stupid naming issue!

The Contractor




msg:438348
 5:51 pm on Dec 18, 2004 (gmt 0)

hehe... it's not, just documented and certain things for security etc.

rocknbil




msg:438349
 6:15 pm on Dec 20, 2004 (gmt 0)

This is using CGI.pm, correct?

I may be off on this part, but try changing

while (read($file,$data,1024))

to

while (read($file,$buffer,1024))

I seem to recall finding $buffer being one of the variables used in CGI.pm that if changed it won't work. (This part may be inaccurate, but it's one variable and you can change it back, try it.)

Secondly, you have verified your substitution indeed grabs the right filename, correct? Beware, this is a double-edged sword - Mac paths do not follow the / syntax. I do something like

@fullpath = split (/\\¦\/¦:/,$file); ## \ for win / for linux : for mac
$filename = $fullpath[$#fullpath]; ## or use pop(@fullpath)

Last thing you could look at is

open (SAVE,">./$filename")

./ from where? I've noticed when working with CGI.pm, your location at the time this bit of code runs is not where you think. I've seen my files turn up in the CGI_temp directory, the server root . . . all kinds of places. Try

$filename = /full_virtual_path/to/$filename;

So all together:

@fullpath = split (/\\¦\/¦:/,$file);
$filename = pop(@fullpath);
$filename = /full_virtual_path/to/$filename;

open (SAVE,">$filename") ¦¦ die $!;
while (read($file,$buffer,1024)) {
print SAVE $buffer;
}
close ($file); ##old habit - maybe bad one but it works
close SAVE;

donahuej




msg:438350
 6:48 pm on Dec 20, 2004 (gmt 0)

Thanks for the reponses - some very good things that I will have to keep in mind as I continue to work on this. It is indeed CGI.pm, which I am just picking up but so far am a huge fan of.

The client in this script would be a windows machine, and the host is *nix, so there is no need to accomodate for macintosh. I have verified that the filename it grabs is correct, as far as I can see anyway, and the script uploads and saves a file fine when I force a filename, so I don't really see how it could be a server-side path issue either. You clearly know more about this than I do, but the only thing I can think of is that after I grab the filename there is some null character or something in it that I can't see that is affecting the script's capacity to use it as a valid file name.

I did try using the $buffer var, and that did not seem to change anything.

Thanks for looking into this, and again, if you see something I'm missing, let me know!

peace,
~Joe Donahue

donahuej




msg:438351
 8:14 pm on Dec 20, 2004 (gmt 0)

kicking myself....

Thank you all for your help, but it turns out the problem the whole time was that I had Taint checking on
(#!/usr/bin/perl -wT is how I was taught to set up the shebang line) for security reasons, and I guess it was so secure it was protecting it from myself! Sounds like something Microsoft would do....

I thought this might have been the issue pretty early on, but dismissed it since I was convinced it was human error.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved