Angharad has asked for the wisdom of the Perl Monks concerning the following question:

Hi there
I am trying to write a interactive webpage. This involves the user uploading a text file to the server and then a perl script running in the background to perform a task and give a response.
Anyway, I'm having problems with the scripts required to uploadthe necessary file.
This is the html form
<HTML> <HEAD></HEAD> <BODY> <FORM ACTION="/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/fo +rm-data"> <h1>Welcome to the Online Server</H1> <br> Please submit your PDB co-ordinate file here. <br> File to upload: <INPUT TYPE="file" NAME="protein"> <br><br> <INPUT TYPE="submit" NAME="Submit" VALUE="Submit File"> </FORM> </BODY> </HTML>
and this is the corresponding form
#!/usr/bin/perl -w use CGI; $upload_dir = "http://myname.tripod.com/mine"; $query = new CGI; $filename = $query->param("protein"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload("protein"); open UPLOADFILE, ">$upload_dir/$filename"; binmode UPLOADFILE; close UPLOADFILE; print $query->header ( ); print <<END_HTML; <HTML> <HEAD> <TITLE>Thanks!</TITLE> </HEAD> <BODY> <P>Thanks for uploading your file!</P> </BODY> </HTML> END_HTML
When I try to run the cgi script via the html form I get the following error
Your script produced this error: <br>Undefined subroutine CGI::upload + at CGI.pm line 349.
Which I don't understand at all because it all seems ok to me.
I was wondering whether the error could be partially due to me using tripod as my server (don't ask ... I've asked for a server for ages at work and have yet to recieve it .. the department one runs php scripts only). apparently tripod is a bit 'odd'.
Anyway .. any help or suggestions much appreciated.

Replies are listed 'Best First'.
Re: CGI script problem .. trying to upload files
by McDarren (Abbot) on Jan 10, 2006 at 14:57 UTC
    Firstly, your error message:

    From the CGI docs:

    To be safe, use the upload() function (new in version 2.47). When cal +led with the name of an upload field, upload() returns a filehandle, +or undef if the parameter is not a valid file- handle.

    You probably have an old version of the CGI module which does not support the upload function.

    If upgrading is not possible, there is an alternative method which is also described in the docs. However, there are several other problems with your code. Here is a short list of just the ones I can see (I'm sure other Monks will point out more)

    • You are not actually printing anything to your UPLOADFILE
    • You are not testing for success when you open your filehandle.
    • You are not using strict. Always use strict - it will help you pick up many errors in your code.
    • You have not enabled taint checking. This is very important whenever your script will accept data from the "outside world"

    I'd strongly recommend you work your way through a few CGI tutorials to learn some more. A couple of good ones:

    Hope this helps,
    Darren :)

Re: CGI script problem .. trying to upload files
by ptum (Priest) on Jan 10, 2006 at 14:34 UTC

    At first I thought the problem was that you didn't import the upload() function ... but then I see you are creating a new instance of a CGI object rather than the function-oriented interface.

    Is it possible your server is working with a very old version of CGI.pm that doesn't have upload()?


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      well there is that possibility .. i dont know how old a cgi version tripod uses :(

        It would have to be ancient - upload() got added in... February 1999(!)