I have read and received conflicting information on how to get the contents of a file that was sent to the server via a form submit. I am presenting a form with the <input type=file name=infile> tag. When the user submits, all I get is the file path.
(code snipet):
ReadParse;
$content = $in{'infile'};
Where content is the path.
I am using <form enctype=multi-part/form-data method=post>.
How do I get the contents of the file instead of the path?
Thank you.
use CGI qw(:standard);
$query = new CGI;
$SAVE_DIRECTORY ="path/to/save/location";
foreach $key (sort {$a <=> $b} $query->param()) {
next if ($key !~ /^uploadfile/);
if ($query->param($key) =~ /([^\/\\]+)$/) {
$Filename = $1;
$Filename =~ s/^\.+//;
$Filename =~ s/\"//;
$File_Handle = $query->param($key);
if (!open(OUTFILE, ">$SAVE_DIRECTORY\/$Filename")) {
print "Content-type: text/html\n\n";
print "Error:\n<br>";
print "File: $SAVE_DIRECTORY\/$Filename\n<br>";
print "Filename: $Filename\n<br>";
exit;
}
undef $BytesRead;
undef $Buffer;
binmode OUTFILE;
while ($Bytes = read($File_Handle,$Buffer,1024)) {
$BytesRead += $Bytes;
print OUTFILE $Buffer;
$filecontents .=$Buffer;
}
close OUTFILE;
}
}
# file is now in $filecontents and stored on disk as $Filename with the byte length in $BytesRead
I am on my way!