Forum Moderators: coopster & phranque

Message Too Old, No Replies

<input type=file> how to get contents

How do I get the contents of a file uploaded to server instead of the path?

         

cat10

10:46 pm on Jun 20, 2001 (gmt 0)



I am a new cgi coder in need of some assistance.

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.

littleman

11:01 pm on Jun 20, 2001 (gmt 0)



We had a discussion a few weeks back that you may find helpful.
[webmasterworld.com...]

Brett_Tabke

3:04 pm on Jun 21, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



What I use is below. Just name your html file upload form value "uploadfile" and put this script as the target:

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

cat10

4:26 pm on Jun 21, 2001 (gmt 0)



Thank you both for your assistance!

I am on my way!