The Wayback Machine - https://web.archive.org/web/20241030045831/https://www.geeksforgeeks.org/perl-file-upload-in-cgi/
Open In App

Perl | File Upload in CGI

Last Updated : 01 Apr, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

 
Image
 

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 :
Image



Previous Article
Next Article

Similar Reads

Perl | CGI Programming
In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests. It is a set of rules and standards that define how the information is exchanged between the web server and custom scripts. Earlier, scripting languages like Perl were used for writing the CGI applications. And, CGI code called by HTTP server was referred to
5 min read
Perl | GET vs POST in CGI
In Perl, Common Gateway Interface (CGI) is nothing more than a protocol which defines the interaction of web servers with some executable programs in order to produce dynamic web pages. Basically, it shows how the web server sends information to the program and the program sends the information back to the web server which in turn can be sent back
4 min read
Perl | Security Issues in CGI
Before diving into the security issues with CGI and Perl, let's know what Perl is. Perl stands for Practical Extraction and Reporting Language. It was built in 1987 by Larry Wall. Its a smart Object oriented language since it takes some useful features from C and BASIC as well. This programming language used to be the master of web programming as i
4 min read
Perl | CGI Security
The CGI stands for the Common Gateway Interface which is a defined protocol for writing dynamic codes on the web. It is also used to execute the scripts online. Perl was accepted as an executable language for HTML pages on the web. Perl is similar to any other CGI language in which, when a code is executed, it creates an interface with the base ope
5 min read
Debugging Perl CGI Scripts
Perl is a cross-platform, open-source computer programming language that is extensively used in both the commercial and private sectors. Perl is popular among Web developers due to its adaptable, ever-evolving text-processing and problem-solving capabilities. Although Perl was initially developed for text editing, its flexibility makes it a versati
5 min read
Perl | Static and Dynamic content in CGI
In the sector of web improvement, Perl has long been a famous choice for growing dynamic content material in CGI (Common Gateway Interface) packages. CGI allows web servers to interact with external packages, allowing the generation of dynamic net pages. In this text, we will explore the standards of static and dynamic content in CGI using Perl, an
3 min read
Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | Searching in a File using regex
Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compatible Regular Expressions". To use the Regex, Bindi
6 min read
Perl | File Locking
File locking, or locking in general, is just one of the various solutions proposed to deal with problems associated with resource sharing. File locking is a method to preserving the security and integrity of any document. The main motive of file locking is allowing people to commit changes to the document without creating a mess of it. When a file
5 min read
Perl | File Test Operators
File Test Operators in Perl are the logical operators which return True or False values. There are many operators in Perl that you can use to test various different aspects of a file. For example, to check for the existence of a file -e operator is used. Or, it can be checked if a file can be written to before performing the append operation. This
3 min read
Perl | Opening and Reading a File
A filehandle is an internal Perl structure that associates a physical file with a name. All filehandles have read/write access, so once filehandle is attached to a file reading/writing can be done. However, the mode in which file handle is opened is to be specified while associating a filehandle. Opening a File Open function is used to open a new f
4 min read
Perl | Appending to a File
When a file is opened in write mode using “>”, the content of the existing file is deleted and content added using the print statement is written to the file. In this mode, the writing point will be set to the end of the file. So old content of file remains intact and anything that is written to file using print statement is added to the end of
2 min read
Perl | Reading a CSV File
Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Reading a text file is a very common task in Perl. For example, you often come across reading CSV(Comma-Separated Value) files to extract data and information. A CSV file c
7 min read
Perl | Writing to a File
A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, ‘>’, “filename.txt”); If the file is existing then it truncates the old content of file with the new content. Otherwise a new file will be created
3 min read
Perl | File Handling Introduction
In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to specify a file mode, which determines how the file can
7 min read
Perl | Accessing a Directory using File Globbing
In Perl, a directory is used to store values in the form of lists. A directory is quite similar to a file. Just like a file, the directory also allows performing several operations on it. These operations are used for the modification of an existing directory or creation of a new one. A directory can be very easily opened and processed using the bu
2 min read
Perl | Useful File-handling functions
Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. These operations can be performed by the use of various inbuilt file functions. Example: #!/usr/bin/perl # Opening a File in Read-only mode open(fh, "<",
2 min read
Perl | File I/O Functions
File handling in Perl is used to read data from an external file or to write data into an external file. This is very useful as it provides a platform to permanently store and retrieve data from files. File Handle A FileHandle associates a name to an external file, that can be used until the end of the program or until the FileHandle is closed. In
4 min read
Perl | eof - End of File Function
The eof() function is used to check if the End Of File (EOF) is reached. It returns 1 if EOF is reached or if the FileHandle is not open and undef in all other cases. Syntax: eof(FileHandle) Parameter: FileHandle: used to open the file Returns: 1 if EOF is reached Cases: eof(FileHandle) : Passing FileHandle to eof() function. If File is empty then
2 min read
Perl | chomp() Function
The chomp() function in Perl is used to remove the last trailing newline from the input string. Syntax: chomp(String) Parameters: String : Input String whose trailing newline is to be removed Returns: the total number of trailing newlines removed from all its arguments Example 1: C/C++ Code #!/usr/bin/perl # Initialising a string $string = &quo
2 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Firstly, they are used in conditionals to determine w
3 min read
Perl | Operators | Set - 1
Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their different functionality: Arithmetic OperatorsRelat
12 min read
Perl | lt operator
'lt' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise less than the string to its right. Syntax: String1 lt String2 Returns: 1 if left argument is less than the right argument Example 1: #!/usr/local/bin/perl # Initializing Strin
1 min read
Perl | chop() Function
The chop() function in Perl is used to remove the last character from the input string. Syntax: chop(String) Parameters: String : It is the input string whose last characters are removed. Returns: the last removed character. Example 1: #!/usr/bin/perl # Initialising a string $string = "GfG is a computer science portal"; # Calling the chop
1 min read
Perl | rename() Function
rename() function in Perl renames the old name of a file to a new name as given by the user. Syntax: rename(old_file_path, new_file_path) Parameters: old_file_path: path of the old file along with its name new_file_path: path of the new file along with its name Returns 0 on failure and 1 on success Example: #!/usr/bin/perl -w # Calling the rename()
1 min read
Perl | Subroutines or Functions
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In Perl, the terms function, subroutine, and method are the same but in s
2 min read
Perl | Decision Making (if, if-else, Nested–if, if-elsif ladder, unless, unless-else, unless-elsif)
Decision Making in programming is similar to decision making in real life. In programming, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance
6 min read
Perl | Loops (for, foreach, while, do...while, until, Nested loops)
Looping in programming languages is a feature which facilitates the execution of a set of instructions or functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Perl provides the different types of loop to handle the condition based situation in the program. The loops in Perl are : for Loop "for" loop
7 min read
Perl | Comparing Scalars
Prerequisite: Scalars in Perl Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars. There are two types of sets of Perl comparison operators. One is for numeric scalar values and one is for string scalar values. Both the types are illustrated in bel
6 min read
Perl | String functions (length, lc, uc, index, rindex)
String in Perl is a sequence of character enclosed within some kinds of quotation marks. Perl string can contain UNICODE, ASCII and escape sequence characters. Perl provides the various function to manipulate the string like any other programming language. Some string functions of Perl are as follows: length() lc() uc() index() rindex() length(): T
4 min read
Article Tags :
three90RightbarBannerImg