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

Dear Perl Monks,
I'm trying to use CGI::Upload to upload a file from the client to a dir on the webserver but can't seem to get it to work. The file gets opened and I can write to it by sending a simple test string. Everything returned from the upload object looks fine. The problem as far as I can see is the file handle returned below into $fh (type IO::File=GLOB) is empty. So what I get when I run this is a 0 length file. Could it be something to do with Mime types ? I'm not sure what to do with this method: mime_magic('/path/to/mime.types'). Should I give this method a list of valid mime types ? I'm not doing this.

Thanks in advance for any help.

Here are the particulars regarding my platform:

OS: XP Pro w/ SP1 Webserver: IIS v5.1 ActivePerl: v5.8.0.0.806 CGI-Upload: v1.05 Use statements: use CGI qw(:standard table*); use CGI::Upload; Start form call: print $q->startform(-method=>'POST',-action=>$q->self_url, -name=>'editform',-enctype=>'multipart/formdata'); File field call: print $q->filefield(-name=>'src',-override=>1,-maxlength=>100); Subroutine: sub fileupload { my $upload = CGI::Upload->new; my $fname = $upload->file_name('src'); my $file_type = $upload->file_type('src'); $upload->mime_magic(); my $mime_type = $upload->mime_type('src'); my $fh = $upload->file_handle('src'); $abfile = "c:\\Inetpub\\wwwroot\\images_test\\test_bill.jpg" ; open (OFH, ">$abfile") or die "Error: File $file_name Upload: $!\n"; while(read($fh, $data, 1024)) { print OFH $data or die "Error writing: $!\n"; } close OFH; }

Replies are listed 'Best First'.
Re: Problem with CGI::Upload using IIS v5.1
by dws (Chancellor) on Nov 16, 2003 at 23:49 UTC
    open(OFH, ">$abfile") or die ...; > binmode(OFH); while ( read($fh, $data, 1024) ) {
    Unless you do this, the bits you upload are going to get mangled by newline conversion.

Re: Problem with CGI::Upload using IIS v5.1
by rob_au (Abbot) on Nov 17, 2003 at 01:46 UTC

    Your usage of the CGI::Upload module in your code appears to be correct, although you should incorporate the binmode action recommended by dws above. Similar examples showing the usage of this module can be found on this site here and here.

    Could it be something to do with Mime types ? I'm not sure what to do with this method: mime_magic('/path/to/mime.types'). Should I give this method a list of valid mime types?
    No, the mime_magic method is for the specification of an alternate MIME types file if you are finding the core set supplied by File::MMagic too limited when using the mime_type method - If you are not using the mime_type method to verify the MIME type of uploaded files, the mime_magic method can be safely ignored.

    If you are still experiencing problems with this module and your web server error logs are unrevealing, feel free to drop me an email and I'll see if I can replicate and solve this problem with you.

    Update: /msg-ed Bgorman with email contact details.

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010001110'"

      I did incorporate the binmode action with the same unsatisfactory result. I tried to replicate the example code also with no luck. Another thing that does not appear to be correct is the return value from the mime_type method. No matter what kind of file I try to upload this method always returns "text/plain". I would like to work with rob_au on this but being new I'm not sure how to make contact through email