CPP   7

fup.cgi

Guest on 12th May 2021 06:10:06 PM

  1. // File Upload Example # 2
  2. // by Matt Flood
  3. // Copyright
  4. //
  5. // this CGI script prompts the user to upload a file
  6. // When submitted, the program returns the users file in a new window.
  7. // The file should not be corrupted by the upload or download.
  8. //
  9.  
  10. #include <rude/cgiparser.h>
  11. #include <iostream>
  12. #include <stdio.h>
  13.  
  14. int main(void)
  15. {
  16.  
  17.     //
  18.     //
  19.     std::cout << "Content-Type: text/html\n\n";
  20.  
  21.    
  22.     // obtain the global CGI Parser object
  23.     //
  24.     rude::CGIParser parser;
  25.  
  26.     std::string message="";
  27.     // Check if a file was uploaded - identified by "testfile"
  28.     //
  29.     if( parser.exists("testfile") && parser.isFile("testfile") )
  30.     {
  31.         // open file for binary write
  32.         //
  33.         FILE *handle = fopen(parser.filename("testfile"), "wb");
  34.  
  35.         if(handle)
  36.         {
  37.             fwrite(parser.value("testfile"), parser.length("testfile"));
  38.             message = "Created file '"  << parser.filename("testfile") << "' for writing";
  39.         }
  40.         else
  41.         {
  42.             message = "Could not open file '" << parser.filename("testfile") << "' for writing";
  43.         }
  44.  
  45.     }
  46.    
  47.  
  48.  
  49.     // Display a form for user to enter file into.  It is a recursive form that points back to this script - named example2.exe
  50.     //
  51.     std::cout << "<html><body>"
  52.             << message
  53.             << "<br>"
  54.             << "<form action='example2.exe' method='POST' enctype='multipart/form-data' target='_new'>\n"
  55.             << "<input type=file name=testfile>\n"
  56.             << "<input type=submit value='upload testfile'>\n"
  57.             << "</form></body></html>\n";
  58.     }
  59.     return 0;
  60. }

Raw Paste


Login or Register to edit or fork this paste. It's free.