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.
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
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.
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.
Example : Sample form to show the Working of File Upload in CGI: