Upload file doesn't work

  • gilbertlsk

    gilbertlsk - 2007-08-21

    I have written an upload.tmpl with <input type="file" name="uploadfile">. And link this tmpl to upload.pl. However, when the cgi->param("uploadfile") is passed in the upload.pl, it doesn't seems to work the way it is supposed to. I think it's only passing as string instead of a filehandle. I tried it without the HTML::Template and it works fine. But when I used the template, it just doesn't work.

    The script for upload.pl is as follow:

    #!/usr/bin/perl -w
    # upload.pl

    use strict;
    use CGI;
    use HTML::Template;

    my $cgi = CGI->new();
    my $template = HTML::Template->new(filename => "upload.tmpl", path => "/var/www/html/");

    my $filename = $cgi->param("uploadfile");
    $filename =~ s/.*[\\/\\](.*)/$1/;
    my @text = <$filename>;
    my $text = join(",", @text);

    $template->param(text => $text);

    print $cgi->header();
    print $template->output();


    The upload.tmpl is as follows:

    <html lang='en'>
    <form method="post" action="/cgi-bin/upload.pl">
    <input type="file" name "uploadfile" size="40" />
    <input type="submit" value="upload" />
    <td><!-- tmpl_var="text" --></td>


    Appreciate the help!! Thanks.

    • Sam Tregar

      Sam Tregar - 2007-08-21

      You must set the form enctype to "multipart/form-data" for file uploads to work:

        <form method="post" action="/cgi-bin/upload.pl" enctype="multipart/form-data">

      I really wish browsers would produce a warning about this, it's bitten me a few times too.


    • gilbertlsk

      gilbertlsk - 2007-08-22

      Thanks a lot Sam. I did put it in my original file. I must have missed it when I put it here. I went back to check again, it was a typo mistake that I've made. Instead of "enctype", I've typed "enctpye". Sorry for the false alarm. Thanks again.

    • Arka Roy

      Arka Roy - 2007-08-23

      One thing I don't get is why it worked when he just used it as an HTML webpage.  The form enctype seems to have become an issue after he made it a template.



Log in to post a comment.