How to upload a file using CGI.pm - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

How to upload a file using CGI.pm

Description:

FORM ENCTYPE='multipart/form-data' ACTION='/cgi-bin/upload.pl' METHOD='POST' Please choose directory to upload to: br SELECT NAME='dir' OPTION VALUE ... – PowerPoint PPT presentation

Number of Views:181
Avg rating:3.0/5.0
Slides: 8
Provided by: sangb
Category:
Tags: cgi | cbs | file | upload | using

less

Transcript and Presenter's Notes

Title: How to upload a file using CGI.pm


1
How to upload a file using CGI.pm
  • http//www.perlfect.com/articles/upload.shtml
  • ACTION"/cgi-bin/upload.pl" METHOD"POST"
  • Please choose directory to upload to

    images/OPTION
  • sounds
  • Please select a file to upload

  • ENCTYPE is a parameter to the form.

2
The CGI script
  • !/usr/bin/perl
  • use CGI
  • my cgi new CGI
  • my dir cgi-param('dir')
  • my file cgi-param('file')

3
After that
  • filem/.(\\\/)(.)/ strip the remote
    path and keep the filename
  • my name 2
  • open(LOCAL, "dir/name") or die !
  • while()
  • print LOCAL _
  • print cgi-header()
  • print "file has been successfully uploaded...
    thank you.\n"

4
Preprocessing URL-encoded CGI input
  • Read the following
  • http//www.perlfect.com/articles/url_decoding.shtm
    l

5
  • GET The input of a GET request is stored in a
    special environmental variable (if you don't know
    what that is, don't worry) called QUERY_STRING To
    access an environmental variable from your
    script, you have to use the ENV hash that perl
    nicely provides. All you need to say is
  • my_input ENVQUERY_STRING
  • and the form's data will be put to my_input.
  • POST The POST method does not put your input in
    some variable. Instead, the web server, upon
    executing your script will pass the CGI input to
    the script from STDIN. (the standard input) So,
    all you have to do is to read from the STDIN
    filehandle to get your input. The only tricky
    thing is that, an EOF (end-of-file) is not
    guaranteed at the end of the string. So how do
    you figure out how much you need to read? Not
    coincidentally, the server puts the length of the
    input string into an environmental variable,
    CONTENT_LENGTH. So you have to read
    CONTENT_LENGTH bytes from STDIN. The line that
    does this is
  • read(STDIN, my_input, ENVCONTENT_LENGTH)

6
How to decode it?
  • The general form is the following
  • field1value1field2value2...fieldnvaluen
  • First of all you'll have to separate each
    field-value pair from the rest. We use the split
    function to do that.
  • _at_fv_pairs split /\/ , my_input

7
to space, and hexacodes into special characters
  • foreach pair (_at_fv_pairs) if(pairm/()(.
    )/)
  • field 1
  • value 2
  • value s/\/ /g value
    s/(\dA-Fa-f2)/pack("C", hex(1))/eg
    INPUTfieldvalue
Write a Comment
User Comments (0)
About PowerShow.com