Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Web Programming

C++ Web Programming

What is CGI?

Web Browsing

To better understand the concept of CGI, let's click a hyperlink to browse a specific web page or URL and see what happens.

However, for an HTTP server set up this way, whenever a file in a directory is requested, the HTTP server does not send back that file but executes it as a program and sends the program's output back to the browser for display.

The Common Gateway Interface (CGI) is the standard protocol that enables applications (called CGI programs or CGI scripts) to interact with the web server and the client. These CGI programs can be written in Python, PERL, Shell, C, or C++, among others.

CGI Architecture Diagram

The diagram below illustrates the architecture of CGI:

Web Server Configuration

Before you do CGI programming, please make sure your web server supports CGI and is configured to handle CGI programs. All CGI programs executed by the HTTP server must be in a preconfigured directory. This directory is called the CGI directory, conventionally named /var/www/cgi-bin. Although CGI files are C++ executables, by convention their extension is .cgi.

By default, the Apache web server is configured to run CGI programs in /var/www/cgi-bin. If you want to specify another directory to run CGI scripts, you can modify the following section in the httpd.conf file:



   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all

 

Options All

Here we assume the web server is already configured and running successfully, and you can run any CGI program, such as Perl or Shell.

Your First CGI Program

Please look at the following C++ program:

Example

#include

<
iostream
using
namespace
std

;

int

main

(

)

{

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Hello World - First CGI Program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Hello World! This is my first CGI program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Compile the code above, name the executable cplusplus.cgi, and save this file in the /var/www/cgi-bin directory. Before running the CGI program, please use chmod 755 cplusplus.cgi the UNIX command to change the file mode and ensure the file is executable. Accessing the executable, you will see the following output:

Hello World! This is my first CGI program

The C++ program above is a simple program that writes its output to the STDOUT file, i.e., displayed on the screen. Here, it is worth noting that the first line of output Content-type:text/html\r\n\r\n. This line is sent back to the browser and specifies the content type to be displayed in the browser window. You must understand the basic concept of CGI in order to further write more complex CGI programs using Python. C++ CGI programs can interact with any other external system (such as an RDBMS).

HTTP Header Information

line Content-type:text/html\r\n\r\n is part of the HTTP header information sent to the browser so that the page content can be better understood. The form of the HTTP header information is as follows:


HTTP 字段名称: 字段内容
 
例如
Content-type: text/html\r\n\r\n

There are some other important HTTP headers that you will frequently use in your CGI programming.

Header Description
Content-type: MIME string defining the format of the returned file. For example, Content-type:text/html.
Expires: Date information becomes an invalid date. The browser uses it to determine when a page needs to be refreshed. The format of a valid date string should be 01 Jan 1998 12:00:00 GMT.
Location: URL This URL refers to the URL that should be returned, not the requested URL. You can use it to redirect a request to any file.
Last-modified: Date The last modified date of the resource.
Content-length: N The length of the data to be returned, in bytes. The browser uses this value to indicate the estimated download time of a file.
Set-Cookie: String Pass string Set a cookie.

CGI Environment Variables

All CGI programs can access the following environment variables. These variables play a very important role when writing CGI programs.

Variable Name Description
CONTENT_TYPE the data type of the content. Used when the client sends additional content to the server, such as file upload.
CONTENT_LENGTH the length of the query information. Available only for POST requests.
HTTP_COOKIE returns the set cookies in the form of key & value pairs.
HTTP_USER_AGENT the User-Agent request header field, which conveys information about the user's request, including the browser's name, version, and other platform-specific additional information.
PATH_INFO the path of the CGI script.
QUERY_STRING the URL-encoded information sent when making a request via the GET method, including the parameters after the question mark in the URL.
REMOTE_ADDR the IP address of the remote host making the request. This is very useful for logging and authentication.
REMOTE_HOST the fully qualified name of the host making the request. If this information is unavailable, REMOTE_ADDR can be used to obtain the IP address.
REQUEST_METHOD the method used to make the request. The most common methods are GET and POST.
SCRIPT_FILENAME the full path of the CGI script.
SCRIPT_NAME the name of the CGI script.
SERVER_NAME the host name or IP address of the server.
SERVER_SOFTWARE the name and version of the software running on the server.

The following CGI program lists all the CGI variables.

Example

#include

<
iostream
#include
<
stdlib.h
#include
<
string
using
namespace
std

;

const

string

ENV

[

24

]

=

{

"

COMSPEC

"

,

"

DOCUMENT_ROOT

"

,

"

GATEWAY_INTERFACE

"

,

"

HTTP_ACCEPT

"

,

"

HTTP_ACCEPT_ENCODING

"

,

"

HTTP_ACCEPT_LANGUAGE

"

,

"

HTTP_CONNECTION

"

,

"

HTTP_HOST

"

,

"

HTTP_USER_AGENT

"

,

"

PATH

"

,

"

QUERY_STRING

"

,

"

REMOTE_ADDR

"

,

"

REMOTE_PORT

"

,

"

REQUEST_METHOD

"

,

"

REQUEST_URI

"

,

"

SCRIPT_FILENAME

"

,

"

SCRIPT_NAME

"

,

"

SERVER_ADDR

"

,

"

SERVER_ADMIN

"

,

"

SERVER_NAME

"

,

"

SERVER_PORT

"

,

"

SERVER_PROTOCOL

"

,

"

SERVER_SIGNATURE

"

,

"

SERVER_SOFTWARE

"

}

;

int

main

(

)

{

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

CGI Environment Variables

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

"

0

\

"

cellspacing =

\

"

2

\

"

"

;

for

(

int

i

=

0

;

i

<

24

;

i

++

)

{

cout

<<

"

\

n

"

;

}

cout

<<

"

\

"

<<

ENV

[

i

]

<<

"

"

;

// try to retrieve the value of an environment variable

char

value

=

getenv

(

ENV

[

i

]

.

c_str

(

)

)

;

if

(

value

!=

0

)

{

cout

<<

value

;

}

else

{

cout

<<

"

The environment variable does not exist.

"

;

}

cout

<<

"

<

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

C++ CGI Library

In a real example, you need to perform many operations through CGI programs. Here is a CGI library written specifically for C++ programs, which we can download from ftp://ftp.gnu.org/gnu/cgicc/ to download this CGI library, and install it by following the steps below:


$ tar xzf cgicc-X.X.X.tar.gz 
$ cd cgicc-X.X.X/ 
$ ./configure --prefix=/usr 
$ make
$ make install

Note:the libcgicc.so and libcgicc.a libraries will be installed in the /usr/lib directory; you need to run the copy command:

$ sudo cp /usr/lib/libcgicc.* /usr/lib64/

so that the CGI program can automatically find the libcgicc.so dynamic link library.

You can click C++ CGI Lib Documentationto view the related library documentation.

GET and POST Methods

You may have encountered a situation where you need to pass some information from the browser to the web server and finally to the CGI program. Usually the browser uses two methods to pass this information to the web server, namely the GET and POST methods.

Passing Information Using the GET Method

The GET method appends encoded user information to the page request. The page and the encoded information are separated by the ? character, as shown below:


http://www.test.com/cgi-bin/cpp.cgi?key1=value1&key2=value2

The GET method is the default way to pass information from the browser to the web server; it generates a long string in the browser's address bar. Do not use the GET method when sending passwords or other sensitive information to the server. The GET method has a size limit: at most 1024 characters can be passed in a request string.

When using the GET method, information is passed using the QUERY_STRING http header, which can be accessed in the CGI program via the QUERY_STRING environment variable.

You can append simply concatenated key-value pairs after the URL, or use HTML

tags with the GET method to pass information.

Simple URL Example: Get Method

Below is a simple URL that uses the GET method to pass two values to the cpp_get.cgi program.

/cgi-bin/cpp_get.cgi?first_name=ZARA&last_name=ALI

The following example generates cpp_get.cgi a CGI program that handles the input given by the web browser. Using the C++ CGI library, the passed information can be easily accessed:

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

formData

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Using GET and POST Methods

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

form_iterator

fi

=

formData

.

getElement

(

"

first_name

"

)

;

if

(

!

fi

->

isEmpty

(

)

&&

fi

!=

(

formData

)

.

end

(

)

)

{

cout

<<

"

First name:

"

<< **

fi

<<

endl

;

}

else

{

cout

<<

"

No text entered for first name

"

<<

endl

;

}

cout

<<

"


\

n

"

;

fi

=

formData

.

getElement

(

"

last_name

"

)

;

if

(

!

fi

->

isEmpty

(

)

&&

fi

!=

(

formData

)

.

end

(

)

)

{

cout

<<

"

Last name:

"

<< **

fi

<<

endl

;

}

else

{

cout

<<

"

No text entered for last name

"

<<

endl

;

}

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Now, compile the program above as follows:


$g++ -o cpp_get.cgi cpp_get.cpp -lcgicc

Generate cpp_get.cgi, place it in the CGI directory, and try to access it using the link below:

/cgi-bin/cpp_get.cgi?first_name=ZARA&last_name=ALI

This will produce the following result:


名:ZARA 
姓:ALI 

Simple Form Example: GET Method

Below is a simple example that uses an HTML form and a submit button to pass two values. We will use the same CGI script cpp_get.cgi to handle the input.

<
form
action
=
"
/cgi-bin/cpp_get.cgi
"
method
=
"
get
"

First name:

<
input
type
=
"
text
"
name
=
"
first_name
"
<
br
/>

Last name:

<
input
type
=
"
text
"
name
=
"
last_name
"
/>
<
input
type
=
"
submit
"
value
=
"
Submit
"
/>
form

Below is the actual output of the form above; please enter your first and last name, then click the submit button to see the result.

Passing Information Using the POST Method

A more reliable way to pass information to a CGI program is the POST method. This method packages information in the same way as the GET method, except that instead of passing it as a text string after the ? in the URL, it passes it as a separate message. This message is passed to the CGI script via standard input.

We also use the cpp_get.cgi program to handle the POST method. Let's use the same example, passing two values via an HTML form and a submit button, but this time using the POST method instead of GET, as shown below:

<
form
action
=
"
/cgi-bin/cpp_get.cgi
"
method
=
"
post
"

First name:

<
input
type
=
"
text
"
name
=
"
first_name
"
<
br
/>

Last name:

<
input
type
=
"
text
"
name
=
"
last_name
"
/>
<
input
type
=
"
submit
"
value
=
"
Submit
"
/>
form

When multiple options need to be selected, we use checkboxes.

The HTML code example below is a form with two checkboxes:

<
form
action
=
"
/cgi-bin/cpp_checkbox.cgi
"
method
=
"
POST
"
target
=
"
_blank
"
<
input
type
=
"
checkbox
"
name
=
"
maths
"
value
=
"
on
"
/>
Math
<
input
type
=
"
checkbox
"
name
=
"
physics
"
value
=
"
on
"
/>
Physics
<
input
type
=
"
submit
"
value
=
"

Select Subject

"

/>

form

The following C++ program generates the cpp_checkbox.cgi script, which handles the input given by the web browser through checkboxes.

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

formData

;

bool

maths_flag

,

physics_flag

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Passing Checkbox Data to a CGI Program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

maths_flag

=

formData

.

queryCheckbox

(

"

maths

"

)

;

if

(

maths_flag

)

{

cout

<<

"

Maths Flag: ON

"

<<

endl

;

}

else

{

cout

<<

"

Maths Flag: OFF

"

<<

endl

;

}

cout

<<

"


\

n

"

;

physics_flag

=

formData

.

queryCheckbox

(

"

physics

"

)

;

if

(

physics_flag

)

{

cout

<<

"

Physics Flag: ON

"

<<

endl

;

}

else

{

cout

<<

"

Physics Flag: OFF

"

<<

endl

;

}

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Passing Radio Button Data to a CGI Program

When only one option needs to be selected, we use radio buttons.

The HTML code example below is a form with two radio buttons:

<
form
action
=
"
/cgi-bin/cpp_radiobutton.cgi
"
method
=
"
post
"
target
=
"
_blank
"
<
input
type
=
"
radio
"
name
=
"
subject
"
value
=
"
maths
"
checked
=
"
checked
"
/>
Math
<
input
type
=
"
radio
"
name
=
"
subject
"
value
=
"
physics
"
/>
Physics
<
input
type
=
"
submit
"
value
=
"

Select Subject

"

/>

form

The following C++ program generates the cpp_radiobutton.cgi script, which handles the input given by the web browser through radio buttons.

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

formData

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Passing Radio Button Data to a CGI Program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

form_iterator

fi

=

formData

.

getElement

(

"

subject

"

)

;

if

(

!

fi

->

isEmpty

(

)

&&

fi

!=

(

formData

)

.

end

(

)

)

{

cout

<<

"

Radio box selected:

"

<< **

fi

<<

endl

;

}

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Passing Text Area Data to a CGI Program

When multi-line text needs to be passed to a CGI program, we use the TEXTAREA element.

The HTML code example below is a form with a TEXTAREA box:

<
form
action
=
"
/cgi-bin/cpp_textarea.cgi
"
method
=
"
post
"
target
=
"
_blank
"
<
textarea
name
=
"
textcontent
"
cols
=
"
40
"
rows
=
"
4
"

Please enter text here...

textarea

<
input
type
=
"
submit
"
value
=
"
Submit
"
/>
form

The following C++ program generates the cpp_textarea.cgi script, which handles the input given by the web browser through the text area.

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

formData

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Passing Text Area Data to a CGI Program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

form_iterator

fi

=

formData

.

getElement

(

"

textcontent

"

)

;

if

(

!

fi

->

isEmpty

(

)

&&

fi

!=

(

formData

)

.

end

(

)

)

{

cout

<<

"

Text Content:

"

<< **

fi

<<

endl

;

}

else

{

cout

<<

"

No text entered

"

<<

endl

;

}

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Passing Dropdown Box Data to a CGI Program

When multiple options are available but only one or two can be selected, we use a dropdown box.

The HTML code example below is a form with a dropdown box:

<
form
action
=
"
/cgi-bin/cpp_dropdown.cgi
"
method
=
"
post
"
target
=
"
_blank
"
<
select
name
=
"
dropdown
"
<
option
value
=
"
Maths
"
selected
Math
option
<
option
value
=
"
Physics
"
Physics
option
select
<
input
type
=
"
submit
"
value
=
"
Submit
"
/>
form

The following C++ program generates the cpp_dropdown.cgi script, which handles the input given by the web browser through the dropdown box.

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

formData

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Passing Dropdown Box Data to a CGI Program

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

form_iterator

fi

=

formData

.

getElement

(

"

dropdown

"

)

;

if

(

!

fi

->

isEmpty

(

)

&&

fi

!=

(

formData

)

.

end

(

)

)

{

cout

<<

"

Value Selected:

"

<< **

fi

<<

endl

;

}

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Using Cookies in CGI

The HTTP protocol is a stateless protocol. But for a commercial website, it needs to maintain session information across different pages. For example, a user finishes registration after completing steps across multiple pages. But how to maintain the user's session information across all web pages.

In many cases, using cookies is the most effective way to remember and track information about user preferences, purchases, commissions, and other data needed for a better visitor experience or website statistics.

How It Works

The server sends some data to the visitor's browser in the form of a cookie. If the browser accepts the cookie, the cookie is stored on the visitor's hard disk as a plain-text record. Now, when the visitor visits another page on the website, the cookie is retrieved. Once the cookie is found, the server knows what was stored.

A cookie is a plain-text data record with 5 variable-length fields:

  • Expires : the expiration date of the cookie. If this field is left blank, the cookie expires when the visitor quits the browser.
  • Domain : the domain name of the website.
  • Path : the path of the directory or web page that set the cookie. If you want to retrieve the cookie from any directory or web page, this field can be left blank.
  • Secure : If this field contains the word "secure", the cookie can only be retrieved via a secure server. If this field is left blank, the restriction does not exist.
  • Name=Value : A cookie is set and retrieved in the form of key-value pairs.

Setting Cookies

Sending cookies to the browser is very simple. These cookies are sent together with the HTTP header, before the Content-type field. Suppose you want to set UserID and Password as cookies; the steps to set cookies are shown below:

Example

#include

<
iostream
using
namespace
std

;

int

main

(

)

{

cout

<<

"

Set-Cookie:UserID=XYZ;

\

r

\

n

"

;

cout

<<

"

Set-Cookie:Password=XYZ123;

\

r

\

n

"

;

cout

<<

"

Set-Cookie:Domain=www.w3cschool.cc;

\

r

\

n

"

;

cout

<<

"

Set-Cookie:Path=/perl;

\

n

"

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Cookies in CGI

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

set cookies

"

<<

endl

;

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

From this example, we learned how to set cookies. We use Set-Cookie the HTTP header to set cookies.

Here, some attributes for setting cookies are optional, such as Expires, Domain, and Path. It is worth noting that cookies are set before the line "Content-type:text/html\r\n\r\n is sent.

Compile the program above, generate setcookies.cgi, and try to set cookies using the link below. It will set four cookies on your computer:

/cgi-bin/setcookies.cgi

Getting Cookies

Retrieving all set cookies is very simple. Cookies are stored in the CGI environment variable HTTP_COOKIE, and their form is as follows:


key1=value1;key2=value2;key3=value3....

The following example demonstrates how to get cookies.

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

cgi

;

const_cookie_iterator

cci

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

Cookies in CGI

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

"

0

\

"

cellspacing =

\

"

2

\

"

"

;

// get environment variable

const

CgiEnvironment

&

env

=

cgi

.

getEnvironment

(

)

;

for

(

cci

=

env

.

getCookieList

(

)

.

begin

(

)

;

cci

!=

env

.

getCookieList

(

)

.

end

(

)

; ++

cci

)

{

cout

<<

"

\

n

"

;

}

cout

<<

"

\

"

<<

cci

->

getName

(

)

<<

"

"

;

cout

<<

cci

->

getValue

(

)

;

cout

<<

"

<

\

n

"

;

cout

<<

"


\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

Now, compile the program above, generate getcookies.cgi, and try to get all available cookies on your computer using the link below:

/cgi-bin/getcookies.cgi

This will produce a list showing the four cookies set in the previous section as well as all other cookies on your computer:


UserID XYZ 
Password XYZ123 
Domain www.w3cschool.cc 
Path /perl 

File Upload Example

To upload a file, the HTML form must set the enctype attribute to multipart/form-data. The input tag with the file type creates a "Browse" button.

<
html
<
body
<
form
enctype
=
"
multipart/form-data
"
action
=
"
/cgi-bin/cpp_uploadfile.cgi
"
method
=
"
post
"
<
p

File:

<
input
type
=
"
file
"
name
=
"
userfile
"
/>
p
<
p
<
input
type
=
"
submit
"
value
=
"
Upload
"
/>
p
form
body
html

The result of this code is the form below:

**Note:** The example above has intentionally disabled saving the uploaded file on our server. You can try the code above on your own server.

Below is the script used to handle file uploads cpp_uploadfile.cpp:

Example

#include

<
iostream
#include
<
vector
#include
<
string
#include
<
stdio.h
#include
<
stdlib.h
#include
<
cgicc/CgiDefs.h
#include
<
cgicc/Cgicc.h
#include
<
cgicc/HTTPHTMLHeader.h
#include
<
cgicc/HTMLClasses.h
using
namespace
std

;

using

namespace

cgicc

;

int

main

(

)

{

Cgicc

cgi

;

cout

<<

"

Content-type:text/html

\

r

\

n

\

r

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

File Upload in CGI

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

// get the list of files to be uploaded

const_file_iterator

file

=

cgi

.

getFile

(

"

userfile

"

)

;

if

(

file

!=

cgi

.

getFiles

(

)

.

end

(

)

)

{

// send data type in cout

cout

<<

HTTPContentHeader

(

file

->

getDataType

(

)

)

;

// write content in cout

file

->

writeToStream

(

cout

)

;

}

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

cout

<<

"

\

n

"

;

return

0

;

}

The example above writes content in the cout stream, but you can open a file stream and save the uploaded file content to a file at the target location.