I am trying to add a file upload to an existing web page.
Every time I upload I get a file that is corrupted.
I made sure to set binmode
on the file handle. I also have my input enctype set to multipart/formdata
in my form.
My code is as follows
$article{upload_file_name} = $cgi->param( 'upFile' );
$article{upload_file} = $cgi->upload( 'upFile' );
if ( $article{upload_file_name} ne "" or $article{upload_file_name} ne null ) {
open( UPLOADS, ">$uploads_dir/$article{upload_file_name}" )
or die "Could not open $uploads_dir/$article{upload_file_name}";
binmode UPLOADS;
while ( <$article{upload_file}> ) {
print UPLOADS;
}
close UPLOADS;
}
I also tried this
$article{upload_file} = $cgi->param( 'upFile' );
if ( $article{upload_file} ne "" or $article{upload_file} ne null ) {
open( UPLOADS, ">$uploads_dir/$article{upload_file}" )
or die "Could not open $uploads_dir/$article{upload_file}";
binmode UPLOADS;
while ( <$article{upload_file}> ) {
print UPLOADS;
}
close UPLOADS;
}
is not doing what you think it is doing. The diamond operator (
<>
) is used to stand in for Perl'sreadline
function, but it has a second meaning where it does the same thing as Perl'sglob
function. And in Perl's parsing rules,<$hash{key}>
is always treated asglob
.perldoc -f perlop
explains:There are at least a couple of workarounds:
Use explicit
readline
call:Assign filehandle to a simple scalar