Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

CGI Uploading (repost)

by Anonymous Monk
on Apr 17, 2003 at 03:12 UTC ( [id://251115]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: CGI Uploading (repost)
by hmerrill (Friar) on Apr 17, 2003 at 12:56 UTC
    If you haven't gotten the answer you're looking for yet, try browsing to

    http://www.tek-tips.com/faqs.cfm?spid=219&sfid=406

    I used to participate in the Perl forum on www.tek-tips.com where the Perl forum has FAQ's that deal specifically with file uploading. Not sure if you have to join first before being allowed to view those FAQ's. HTH.
Re: CGI Uploading (repost)
by tune (Curate) on Apr 17, 2003 at 04:03 UTC
    You must be a newbie with search engines, learn to use them they are quite fantastic!

    Here is what I have found for you: http://stein.cshl.org/WWW/software/CGI/

    Open it in your browser, and search for the "upload" word. Now you must succeed.

    --
    tune

      That documentation is nearly identical to all the other's, that's what I've come to notice about perl. All documenations are the same so if you don't understand one you're in a lot of trouble.

      One of the things I don't get is where $filehandle comes from.

      while (<$filename>) { print; }
      Is that a built in variable or does it automatically assign itself from the upload field or do I need to assign it or what? It doesn't cover that anywhere. I guess this was the main part I couldn't understand.

        It can be tricky to see the right information when it is all new to you... It actually does explain where $filename comes from on that page, and in the CGI.pm perldocs.

        $filename = $query->param('uploaded_file');

        Where 'uploaded_file' is the name of the form parameter that you put in your HTML file. Right around the same place where the above code appears, it also says the following:

        The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:

        What this means is that the $filename that is returned in the above code actually contains 2 different things. When you look at $filename as a scalar, you will get a string containing the name of the file that was uploaded. However, if you try to use the $filename as a file handle, then you can read from it and get the contents of the file. There is some perl 'magic' going on here that you should not worry about right now, but should look into later when you are more familiar with Perl (You can read the perldata manpage and search for the section on TypeGlobs and Filehandles for more info on what is happening here).

        So, to get back to your question, to get a file upload to work, you need to do something like the following:

        # Get the name of the file and an # open file handle to the file contents $filename = $query->param('uploaded_file'); # Read a text file and print it out while ($line = <$filename>) { print $line; }

        Or if the uploaded file is a binary file like an GIF or JPEG:

        # Get the name of the file and an # open file handle to the file contents $filename = $query->param('uploaded_file'); # Copy a binary file to somewhere safe open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; } close $filename;

        The way the file upload actually works is that the web server receives the file and stores it in a temporary place. The CGI.pm module opens up this file for reading and gives you the open filehandle. You have to use this filehandle to read the contents of the file, and then do something with it. Usually this just means creating a new file and writing the contents to it.

        Again, this is straight out of the documentation, but I am just trying to point you in the right direction. Feel free to ask more question if you get stuck, but the best way to learn is just to try a bunch of things and see what happens.

        Good Luck

        Update: I forgot about the 'upload' method that chromatic mentions above, so definately look at that as well.

        $filename comes from just a few lines earlier in the documentation. Of course, a few lines later, the docs suggest this code instead:

        use CGI; my $query = CGI->new(); my $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }

        Assuming you've created a form that posts to the script correctly, uses the POST method, uses multipart/form-data encoding, and contains a file upload field named uploaded_file, this will work for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://251115]
Approved by sulfericacid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found