in reply to File upload under Apache2

Everything? perldoc CGI

http://search.cpan.org/~lds/CGI.pm-3.49/lib/CGI.pm#PROCESSING_A_FILE_UPLOAD_FIELD

$lightweight_fh = $q->upload('field_name'); # undef may be returned if it's not a valid file handle if (defined $lightweight_fh) { # Upgrade the handle to one compatible with IO::Handle: my $io_handle = $lightweight_fh->handle; open (OUTFILE,'>>','/usr/local/web/users/feedback'); while ($bytesread = $io_handle->read($buffer,1024)) { print OUTFILE $buffer; } }
or
$filename = $query->param('uploaded_file'); $tmpfilename = $query->tmpFileName($filename);

Replies are listed 'Best First'.
Re^2: File upload under Apache2
by Bruce32903 (Scribe) on Aug 16, 2010 at 04:20 UTC
    Thank you for the reply.

    I have spent about 20 minutes trying to get it to work. Initially I did not have anything similar to $q = CGI ->new; in my code. Adding that and your suggestion initially caused server errors to be posted on the browser. After debugging several minor mistakes and/or cut and paste issues on my part I now have no server errors, but it still does not seem to work.

    I'll keep plugging away at it, but any other suggestions would be appreciated. Having to add $q = CGI ->new; and dealing with lightweight and regular file handles is stretching my perl knowledge. Up until the file upload, the various submit buttons, text fields, radio buttons, etc. were all working well without them.

    Thanks,
    Bruce

        I think the "How Not to Ask a Question" link is almost a little bit unsuitable in this case because the OP asked a clear question but seems to have been confused by online tutorials that cover PHP rather than Perl.

        @Bruce32903, I think the error you got when using the example posted above is most likey due to copying the path given in the example, which will (most likely) not exist on your system. Just change '/usr/local/web/users/feedback' to something that does exist on your system and make sure that you end up with a unique filename. For example, if you can identify your user by a user ID then you might want to write the uploaded file to

        'some/path/to/uploaded/files/USER_XXX/feedback'

        You only need to understand how to read and write files to the disk. When asked to upload a file, your web server will save the file in a temporary location on your filesystem and all you get from CGI is either a handle on that file (this is the first example in the first reply to your post) or the full name to the temp file (second example).

        Once you have that handle, you read from it like you would read from any other file on the disk in a "while (<FILEHANDLE>){..}" loop and you can either write to a new file or jsut do something with the informatin you got and let the system automatically dispose of the tmp file if you don't need to store it.

        Just one more thing: if you happen to be on Windows, you might need to add a line

        binmode OUTFILE;
        after the "open" statement in the example in the first reply. As far as I know, it doesn't do any harm to put it in just in case anyway.