Perl | File Upload in CGI

Perl is general-purpose programming language, specially designed for text manipulation and in present time used for various task including system administration, web development, network programming, GUI development, and in other more area.

In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests or in other words we can say it’s a set of rules and standards that define how information is exchanged between the custom script and Web Server.

CGI Architecture Diagram

 

 

How CGI uploads the file

 
At the Web server end, the software (in our case, a CGI script) interprets the form data that’s sent from the browser, and extracts the file name and contents, along with the other form fields. Usually, the file is then saved to a directory on the server.

Note: Following are the necessities to build a CGI Upload Script:

  • Access to a CGI compatible Web Server
  • A copy of Perl must be running on the Web server
  • Your Web server must contain the Perl CGI Library, CGI.pm

Uploading a file on the Web Server is done with the use of a File upload Form. This File upload form is created on any text editor available and the form must be saved with .htm or .html extension. Creation of a File Upload form involves the following steps:

Step 1: Creating Form element
Firstly, there is a need to create a ‘form’ element

filter_none

edit
close

play_arrow

link
brightness_4
code

<form action="/cgi-bin/upload.cgi" method="post" enctype="multipart/form-data">

chevron_right


Note: In the above code, the multipart/form-data is the encoding type to be used for File Upload and the file upload.cgi is used to store the data posted using this form

Step 2: Creating Form fields
Secondly, we need to provide fields for the form. These fields are used to guide the user with the files that need to be uploaded in the form.
e.g. Here, we will provide an Upload field for the user to upload their photos and to provide their Email.

filter_none

edit
close

play_arrow

link
brightness_4
code

<p>Upload Photo: <input type="file" name="photo" /></p>
<p>Email: <input type="text" name="email_address" /></p>

chevron_right


Step 3: Providing Submit Form button
Third Step is to allow the user to submit all the files uploaded according to the fields given in the form. For that, a submit button is required so that the user can send the form to the Web Server.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!--using button for sending the form to web server-->
<p><input type="submit" name="Submit" value="Submit Form" /></p>

chevron_right


 
Example : Sample form to show the Working of File Upload in CGI:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html lang="en"
<head>
    <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> 
    <title>File Upload</title>
</head
<body style = "text-align:center;"
    <h1 style = "color:green;"> GeeksForGeeks </h1
    <form action = "/cgi-bin/upload.cgi"
          method = "post" enctype = "multipart/form-data">
    <p>Upload Photo: <input type = "file" name = "photo" /></p>
    <p>Email: <input type = "text"
                     placeholder = "e.g. GFG@gmail.com"
                     name = "email_address" /></p>
    <p><input type = "submit" name = "Submit" value = "Submit Form" /></p>
    </form
</body>
</html>                    

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.