I am trying to do a simple upload of a .csv file and save it on my server. I'm not an expert in HTML or Perl, but due to client constraints this is what I must do.
Here is the HTML:
<form action="/path/to/service" target="_self" method="POST" enctype="multipart/form-data">
File: <input type="file" name="attachment" size="50">
<SUBMIT_RESET Upload File>
</form>
The Perl code looks like this:
my $sql = "SELECT NOW()";
my $date = $DB->get_field($sql);
my ($path, $ext) = split(/\./, $in{'attachment'});
my @pathParts = split(/\//, $path);
my $filename = $pathParts[@pathParts - 1] . " - " . $date;
if ($ext eq "csv") {
open (OUTFILE, ">", "$datadir/imports/" . $filename . "." . $ext);
while (read($in{'attachment'}, $buffer, 1024)) {
$file .= $buffer;
}
print OUTPUT $file;
close (OUTFILE);
}
Can anyone please give me some direction as to what I'm doing wrong. I get the file located at the correct path, but it's always empty.
Now, the code base I'm dealing with here is horrible and I cannot use strict.
Suggestions?
EDIT1: To attempt to answer how $in{'attachment'} is populated I've included a snippet of code of how forms are handled.
if ($ENV{'CONTENT_TYPE'} =~ m#^multipart/form-data#) {
my $cgi = new CGI;
my @names = $cgi->param;
foreach $name (@names) {
my @value = $cgi->param($name);
if (scalar(@value) > 1) {
foreach $val (@value) {
$in{$name} .= $val."\0";
}
$in{$name} =~s/\\0$//;
} else {
my $value = $cgi->param($name);
#my $value = join "\0", @value;
$in{$name} = $value;
}
#my $value = $cgi->param($name);
#my $value = join "\0", @value;
#$in{$name} = $value;
}
EDIT2:
To summarize the solution provided by ikegami...
I missed that the file handles were different for the read and print statements - OUTFILE and OUTPUT. Clearly that is completely a novice mistake and for that I apologize!
Best wishes to all who tried to help me.
$in{'attachment'}populated?{ use strict; ... }) including in a sub (sub foo { use strict; ... }) without affecting anything around it.$in{'attachment'}populated? specifically! You didn't provide your input, and the problem is your input.