C/C++ CGI File Upload

A long time ago when I still had (some) hair and hadn’t bitten the PHP bullet I played around with C++ CGIs. Owing to a lack of then available HOW-TO docs I went on to write a (badly written and error-filled) CGI in C/C++ HOW-TO and also a CGI Variable Wrapper. The HOW-TO did what it said on the tin and the wrapper provided an easy API to read/write GET and POST variables as well as cookies.

Surprisingly both the HOW-TO and the wrapper are still in use and I get contacted form time to time with queries. The most common query regards file upload which the wrapper doesn’t support. To illustrate a simple file upload I cobbled together a quick and dirty C example which I’ve provided via email ever since.

So here, for general reference, is my demonstration C code. Please note this is very untested and unrobust, even dodgier than my usual fare. I keep meddling with the idea of finding time to do a proper job either of a standalone file upload API or integrating support into the CGI wrapper. All of this is really just for kicks though as there are better solutions available.


3 thoughts on “C/C++ CGI File Upload

  1. Peter Konde

    Für die Linuxer muss der Befehl in Zeile 35 & 55 (bei Printf) nicht nur %u heißen, sondern %lu (long unsigned).

  2. Peter Konde

    For Linux Operating Systems the “%u” in line 35 & 55 (printf) replaced by “%lu” (–> long unsigned int)

  3. Vijay Kanta

    Nice write up. But, I see this only handles one single input which is of the file. Can you give a more elaborate example when a resume upload form is handled? I mean multiple text input and one file input.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.