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

Hi,
I've been asked to write a program in perl which receives
an uploaded file, parses it and does some database
operations. I've written the program and it works fine
on my machine (my machine is a Windows NT and has IIS
4.0 as well as Perl 5.6 on it). the problem started
when i moved my perl file to another machine (which is
a test server), and tried to upload a file which was on
my machine's hard drive to the test server.The perl
program runs through without generating any error, but
does not receive the uploaded file's data,i.e. all the
other parameters which i pass to the perl file via the
POST method are being passed correctly, except the text
file which i am uploading.
If i go to the test server and upload a text file which
resides on its hard drive, then the perl program runs
fine. but if it is from one machine to another separate
machine, then the uploaded file seems to be getting
lost somewhere.
I even tried changing the name of the temporary directory
name which is in the CGI.pm file, but to no avail.

Any suggestions ?

Replies are listed 'Best First'.
(Ovid) Re: File Upload Problem
by Ovid (Cardinal) on Oct 27, 2000 at 09:36 UTC
    Here's a couple of guesses:
    • You didn't use -w
    • You didn't use strict
    • You didn't check the return code on your open
    • And here's my bet: your form tag does not specify enctype="multipart/form-data"
    The last one can directly cause the problem you've mentioned. Typically, when you have an input type of file, the form tag and input tag should look something like the following:
    <form method="post" action="somescript.cgi" enctype="multipart/form-da +ta"> <input type="file" name="file">
    The enctype in this case specifies that the form data be sent in MIME format. This is the ONLY way that your system can parse out the upload contents. However, the value associated with the parameter "file" is something like "C:\windows\desktop\somefile.txt". Trying to open this filehandle directly when using strict will actually kill your script. However, if you try to open the filehandle directly on a local machine without strict, you'll actually get the file you are looking for, because Perl finds the path to the file. CGI.pm actually takes the file and stores it on the server and returns the param as a filehandle to the stored file. (I know I didn't explain this well, I'm a bit tipsy :)

    See this node for a file upload script that I fixed up for another Monk. It may help you get started in the right direction.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

      I did a lot of similar programs for a client recently. The biggest reason for an upload not happening IMHO is the last one you suggested. Without the enctype tag most browsers are unsure just what to pass and seem to just pass the filename as a text string. While this may be what you want in some cases, it wont be in most.
Re: Fil Upload Problem
by wardk (Deacon) on Oct 27, 2000 at 07:01 UTC

    Just a guess without code to look at, but you may be try to perhaps open the $filename you are getting? ala

    my $filename = $query->param("file_uploaded"); open(FH, "<$filename or die "cannot open!");

    If so, don't do that. Just process the $filename which is a file handle.

    my $filename = $query->param("file_uploaded"); while (<$filename>) { # do something }

    well I hope I guessed right :-), good luck!

      Edit: wrong node
Re: Fil Upload Problem
by AgentM (Curate) on Oct 27, 2000 at 07:45 UTC
    If you say that everything passed to the CGI POST method is correct except for the fact that the file data remains unsent, then double check that you have a file sending capable browser (I hope you're using the same browser for these tests). Perhaps your statement is incorrect. Just like wardk suggested- double check the data with a length() before you write it to a file. Perhaps there is a discrepancy with binmode(). Unfortunately, the bare bones description taken literally (perhaps you still need to double-check some values before processing) offers no other solutions.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Fil Upload Problem
by mirod (Canon) on Oct 27, 2000 at 14:11 UTC

    I don't know if that would explain the behaviour of your script, I haven't had problems with uploading files in a while, but if you are using an old version of Perl on the other machine the problem might come from an old version of CGI.pm being bundled with Perl, one that did not implement the file upload feature.

    try the following code on both machines:

     perl -e 'use CGI; print "CGI version: $CGI::VERSION\n";'

    and see if the versions differ widely.