Open In App

Perl | File Upload in CGI

Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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.




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


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.




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


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




<!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>                    


Output :



Last Updated : 01 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads