I need to add file upload capabilities to a CGI script, but am having problems. I know that normally the best way to go would be to use CGI.pm's upload function, however this is not an option, as this requires Fcntl, which uses Dynaloader, which I am unable to use on this system. I am, however, able to use most of the rest of CGI.pm's functions, such as getting params. I have also been able to work with files without needing Fcntl for other things like reading, writing and copying files on the server.
To prevent the script from failing on load, I have file upload disabled in CGI.pm (via the $DISABLE_UPLOADS flag).
The submit form is a basic form with a file element named sscfFile, which uses POST and enctype="multipart/form-data". The files that will be uploaded through this form are CSV files.
As far as the processing script, here is what I have. It is currently not working because the
$query->param('sscfFile') line doesn't give any value, presumably because it is being ignored due to uploads being disabled. I believe that I also need to extract the file data to a buffer, since CGI isn't handling that in this case.
#!/usr/bin/perl -w
use CGI();
my $query = new CGI;
$upload_dir = "/tmp";
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title>BFT File Upload</title></head>\n";
print "<body>\n";
print "Processing Uploaded File...<br />";
$sscfFile = $query->param('sscfFile') || '';
if ($sscfFile =~ /([\w .-]+)$/i) { #strip off path stuff
$sscfFile = $1;
}
else {
error("Could not generate filename $!");
}
open (UPLOADFILE, ">$upload_dir/$sscfFile") || (error("Cannot open fil
+e $upload_dir/$sscfFile $!"));
binmode UPLOADFILE;
while ($bytesread=read($sscfFile,$buffer,1024)) {
$totalbytes += $bytesread;
print UPLOADFILE $buffer;
}
close UPLOADFILE;
print "</body></html>";
I looked around at the other threads on this site related to uploads, but they all seem to use CGI.pm for upload. I think what I need to do is manually extract the multipart form data into a buffer, and then use code similar to above to write it to a file. Does anyone have suggestions as to how I can go about this?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.