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
[edited by: donahuej at 2:44 pm (utc) on Dec. 18, 2004]
So weird....
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;
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
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.