Randy Randy - 1 month ago 5x
Perl Question

Perl CGI Input type FILE

I'm trying to do a simple Perl CGI script ..where users can upload a excel file on a webpage ..which will get stored in a folder and later I will do some operation.

I am able to create webpage with a text box using "textarea" syntax..Below is my working code .

sub output_form {
my ($q) = @_;
print $q->start_form( -name => 'main', -action => 'cgi.cgi', -method => 'POST', -enctype => 'multipart/form-data');
print $q->start_table;
print $q->Tr(
$q->td(' Upload the spreadsheet here :'),
$q->textarea(-name => "text", -size => 200, -cols => 30, -rows => 5 )

print $q->Tr( $q->td( $q->submit( -value => 'Submit' ) ),
$q->td(' ') );
print $q->end_table;
print $q->end_form;


However , my problem is I want the Input type of file ..so that I can ask users to browse and attach a spreadsheet..which I'm not able to do any help will be appreciated.

there are few example on internet..which are HTML ..but I want a CGI way


there are few example on internet..which are HTML ..but I want a CGI way

This sounds like you are slightly confused. There are two steps here. Firstly, you need to create the correct HTML input and then, later, you need to process that input to upload the file. These two stages happen at two completely separate times in response to two separate HTTP requests. It looks like this:

  • Step 1
    • HTTP Request - User visits your web page.
    • HTTP Response - Your web app returns HTML which includes the file input.
  • Step 2
    • HTTP Request - User fills in and submits the form.
    • HTTP Response - Your web app processes the request, does whatever it needs to with the file, and then returns HTML to the user saying something like "Thank you. Your file has been received".

So, step 1. How do you create the file input operator. I suspect that by "I want a CGI way" you mean "how do I do this using the HTML generation functions?" Firstly read this from the official CGI.pm documentation - HTML Generation functions should no longer be used. We have known that the HTML generation functions that used to be part of CGI.pm were a terrible idea for at least fifteen years. No-one should be using them now. That's why documentation for those functions was removed two years ago. Please use a templating system instead.

But ultimately, it doesn't really matter. If you insist, you can still use the HTML generation functions (the documentation is here) or you can be sane and use a template which includes the file input. Your form could even be in a static HTML file. Whatever approach you choose, you will end up returning HTML to the browser which contains something like this:

<input type="file" name="file_upload"  />

(See, it's not really hard. The HTML generation functions really make life harder than it needs to be!)

Then, once the use has filled in the form and hit "submit", you need to process that input. The documentation for doing that is here. But, to summarise, you can get the name of the file using:

my $filename = $q->param('file_upload');

Or, probably more useful, you can get an open filehandle on the contents of the file from:

my $io_handle = $q->upload('file_upload');

Which means that you can write the file to somewhere on your server with code like this:

if ( my $io_handle = $q->upload('file_upload') ) {
  open ( my $out_file, '>>', $some_file_path ) or die $!;
  while ( my $bytesread = $io_handle->read($buffer,1024) ) {
    print $out_file $buffer;

You can also get the MIME type of the uploaded file with:

my $type = $q->uploadInfo( $io_handle )->{'Content-Type'};

But I recommend that you read all of the documentation in order to pick up all of the details.

p.s. Oh, and CGI? In 2016?! Please look at something a little more up to date.