By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Uploading file to a server

P: 8
Hi,
I have a C++ function which sends a post request to a server for uploading a file.
I have also written a server side cgi to receive the data and create the file in a specified directory.
The server side cgi creates a file, however it is blank, I don't see any errors in the log files (apache2/error.log)
Below is my server side script
Expand|Select|Wrap|Line Numbers
  1. !/usr/bin/perl -wT
  2.  
  3. use strict;
  4. use CGI;
  5. use CGI::Carp qw ( fatalsToBrowser );
  6. use File::Basename;
  7.  
  8. $CGI::POST_MAX = 10240 * 5000;
  9. my $safe_filename_characters = "a-zA-Z0-9_.-";
  10. my $upload_dir = "/usr/local/web/upload";
  11.  
  12. my $query = new CGI;
  13. my $filename = $query->param("flat_file");
  14. #my $email_address = $query->param("email_address");
  15.  
  16. if ( !$filename )
  17. {
  18.  print $query->header ( );
  19.  print "There was a problem uploading your file (try a smaller file).";
  20.  exit;
  21. }
  22.  
  23. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
  24. $filename = $name . $extension;
  25. $filename =~ tr/ /_/;
  26. $filename =~ s/[^$safe_filename_characters]//g;
  27.  
  28. if ( $filename =~ /^([$safe_filename_characters]+)$/ )
  29. {
  30.  $filename = $1;
  31. }
  32. else
  33. {
  34.  die "Filename contains invalid characters";
  35. }
  36.  
  37. my $upload_filehandle = $query->upload("flat_file");
  38.  
  39. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
  40. binmode UPLOADFILE;
  41.  
  42. while ( <$upload_filehandle> )
  43. {
  44.  print UPLOADFILE;
  45. }
  46.  
  47. close UPLOADFILE;
  48.  
  49. print $query->header ( ); 
  50.  
Appreciate if anybody can point out what I am doing wrong in the script.
I can post my C++ code also if needed, it's a bit longer so I am not posting it right now.

Thanks,
Sep 16 '08 #1
Share this Question
Share on Google+
11 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
You have:

Expand|Select|Wrap|Line Numbers
  1. print UPOADFILE;
  2.  
but you aren't telling it what to print. If you want to use the default variable, then put it in there, like so:

Expand|Select|Wrap|Line Numbers
  1. print UPOADFILE ("$_\n");
  2.  
Regards,

Jeff
Sep 16 '08 #2

P: 8
You have:

Expand|Select|Wrap|Line Numbers
  1. print UPOADFILE;
  2.  
but you aren't telling it what to print. If you want to use the default variable, then put it in there, like so:

Expand|Select|Wrap|Line Numbers
  1. print UPOADFILE ("$_\n");
  2.  
Regards,

Jeff
Hi jeff,
Thanks for the quick reply, I modified the script as per your suggestion, still the uploaded file is blank.
Thanks,
Mohit
Sep 16 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
what is the enctype of the <form> tag?
Sep 16 '08 #4

P: 8
what is the enctype of the <form> tag?
Here is my C++ code which sends a post request to the server for file upload. I am using Content-Disposition: multipart/form-data
Expand|Select|Wrap|Line Numbers
  1. int Send_to_DB_via_HTTP(char* Traveller_output_buffer, unsigned data_len, string fileName, string servIP, string url)
  2. {
  3.  
  4.      int sock;                          /*  Socket descriptor */
  5.         struct sockaddr_in echoServAddr;   /*  server address */
  6.         unsigned short echoServPort;       /*  server port */
  7.         //char *servIP;                      /*  Server IP address (dotted quad) */
  8.     //char *url;
  9.         char *echoString;                  /*  String to send to echo server */
  10.         char echoBuffer[RCVBUFSIZE];       /* Buffer for echo string */
  11.         unsigned int echoStringLen;        /* Length of string to echo */
  12.         int bytesRcvd, totalBytesRcvd;     /* Bytes read in single recv()
  13.                                                    and total bytes read */
  14.     int                         first_form_len=0;
  15.         int                         second_form_len=0;
  16.     unsigned             count = 0;
  17.     int    count1 = 0;
  18.     int    count2 = 0;
  19.     char buf[2000];
  20.     int n;
  21.     const char *m_servIP;
  22.     const char *m_url;
  23.     m_servIP = servIP.c_str();
  24.     m_url = url.c_str();
  25.     //servIP = "192.168.2.20";
  26.     echoServPort = 80;
  27.     //url = "http://tewaris.dyndns.org/";
  28.  
  29.      /*Create a reliable, stream socket using TCP */
  30.  
  31.     if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  32.           cout << " socket () failed" << endl;
  33.  
  34.     /* Construct the server address structure */
  35.  
  36.     memset(&echoServAddr, 0, sizeof(echoServAddr));         /* Zero out structure */
  37.     echoServAddr.sin_family         = PF_INET;              /* Internet address family */
  38.     echoServAddr.sin_addr.s_addr = inet_addr(m_servIP);       /* Server IP address */
  39.     echoServAddr.sin_port           = htons(echoServPort); /* Server port */
  40.  
  41.     /* Establish the connection to the echo server */
  42.     int k;
  43.     k = connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr));
  44.     //cout << k << endl;
  45.     //if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
  46.     if (k < 0)
  47.     {
  48.           cout<< "Error Connecting to " << servIP << endl;
  49.     return -1;
  50.     }
  51.     else
  52.      cout << "Connection Successfull to " << servIP << endl;
  53.  
  54.      //Start the form off with the boundary string
  55.  
  56.     first_form_len += snprintf(
  57.                         &buffer1[first_form_len],
  58.                         sizeof(buffer1) - first_form_len,
  59.                         "%s%s\r\n",
  60.                         "--",BOUNDARY_STRING );
  61.     //Some random filename - must end in .dat because web server checks
  62.     //This is the data portion of the form - the flat file will be sent in this section
  63.     first_form_len += snprintf(
  64.                         &buffer1[first_form_len],
  65.                         sizeof(buffer1) - first_form_len,
  66.                         "Content-Disposition: form-data; name=\"flat_file\"; filename=\"travellerdata.dat\"\r\n" );
  67.  
  68.     first_form_len += snprintf(
  69.                         &buffer1[first_form_len],
  70.                         sizeof(buffer1) - first_form_len,
  71.                         "Content-Type: text/plain\r\n\r\n" );
  72.  
  73.  
  74.     //Prepare the ending part of the form-data - this follows the flat file contents
  75.  
  76.  
  77.     //Start the form off with the boundary string
  78.     second_form_len += snprintf(
  79.                         &buffer1[first_form_len+second_form_len],
  80.                         sizeof(buffer1) - (first_form_len+second_form_len),
  81.                         "%s%s\r\n",
  82.                         "--",BOUNDARY_STRING );
  83.     //Not sure why they have this hidden field, but we need to include it or else
  84.     //the web server will reject
  85.     //char system_serial_number[SERIAL_NUMBER_MAX_STRING_LENGTH+1];
  86.     //Get_serial_number_string( system_serial_number, sizeof(system_serial_number) );
  87.     second_form_len += snprintf(
  88.                         &buffer1[(first_form_len+second_form_len)],
  89.                         sizeof(buffer1) - (first_form_len+second_form_len),
  90.                         "Content-Disposition: form-data; name=\"filename\"\r\n\r\n%s_auto_end\r\n",
  91.                         syssn_no );
  92.  
  93.     //End of multi-part form
  94.     second_form_len += snprintf(
  95.                         &buffer1[(first_form_len+second_form_len)],
  96.                         sizeof(buffer1) - (first_form_len+second_form_len),
  97.                         "%s%s%s\r\n",
  98.                         "--",BOUNDARY_STRING,"--" );
  99.  
  100.  
  101.     // Put together the headers for HTTP POST
  102.     count = snprintf(
  103.                        &buffer[0],
  104.                        sizeof(buffer),
  105.                        "POST %s HTTP/1.1\r\n",
  106.                        m_url);
  107.     /*count = snprintf(
  108.                        &buffer[0],
  109.                        sizeof(buffer),
  110.                        "Connection: %s Keep-Alive\r\n",
  111.                        "");*/
  112.     count += snprintf(
  113.                         &buffer[count],
  114.                         sizeof(buffer) - count,
  115.                         "Accept-Language: en-us\r\n" );
  116.     count += snprintf(
  117.                         &buffer[count],
  118.                         sizeof(buffer) - count,
  119.                         "Content-Type: multipart/form-data; boundary=%s\r\n",
  120.                         BOUNDARY_STRING);
  121.  
  122.  
  123.     count += snprintf(
  124.                         &buffer[count],
  125.                         sizeof(buffer) - count,
  126.                         "User-Agent: Mozilla/3.01 (compatible)\r\n");
  127.     count += snprintf(
  128.                         &buffer[count],
  129.                         sizeof(buffer) - count,
  130.                         "Host: %s\r\n",
  131.                         m_servIP);
  132.  
  133.  
  134.     count += snprintf(
  135.                         &buffer[count],
  136.                         sizeof(buffer) - count,
  137.                         "Pragma: no-cache\r\n" );
  138.     count += snprintf(
  139.                         &buffer[count],
  140.                         sizeof(buffer) - count,
  141.                         "Content-Length: %d\r\n",
  142.                         data_len+first_form_len+second_form_len );
  143.     count += snprintf(
  144.                         &buffer[count],
  145.                         sizeof(buffer) - count,
  146.                         "Connection: Keep-Alive\r\n" );
  147.     count += snprintf(
  148.                         &buffer[count],
  149.                         sizeof(buffer) - count,
  150.                         "\r\n" );
  151.     //cout << buffer << endl;
  152.    count1 += snprintf(
  153.             &buffer2[count1],
  154.             sizeof(buffer2) - count1,
  155.             "%s%s\r\n",
  156.                         "--",BOUNDARY_STRING);
  157.  
  158.    count1 += snprintf(
  159.             &buffer2[count1],
  160.             sizeof(buffer2) - count1,
  161.             "Content-Disposition: multipart/form-data; name=\"flat_file\";filename=\"travellerdata.dat\"\r\n",
  162.             "\r\n");
  163.    count1 += snprintf(
  164.             &buffer2[count1],
  165.             sizeof(buffer2) - count1,
  166.             "Content-Type: text/plain\r\n");
  167.    count1 += snprintf(
  168.             &buffer2[count1],
  169.             sizeof(buffer2) - count1,
  170.             "Content-Length: %d\r\n", data_len+count1);
  171.    count2 += snprintf(
  172.             &buffer3[count2],
  173.             sizeof(buffer3) - (count1+count2),
  174.             "%s%s%s\r\n",
  175.             "--",BOUNDARY_STRING, "--");
  176.  
  177.     string status;
  178.  
  179.     cout << "Sending Post Headers" << endl<< buffer << endl << "End Post Headers" << endl;
  180.  
  181.     //Send the "POST" header to the HTTP server.
  182.     if (send(sock, buffer, count, 0) == -1) {
  183.  
  184.         //status = perror("send");
  185.         cout << "Stats Worker: received socket error %d on header send\n" << endl;
  186.         return -1;
  187.     }
  188.     /*else
  189.     {
  190.         cout << "data Sent" << endl;
  191.     }*/
  192.  
  193.     cout << "Now Sending first part of the form data BUFFER1" << endl << buffer1 << endl<< "End First Part"<< endl;
  194.  
  195.  
  196.  
  197.     if (send(sock, buffer1, first_form_len, 0) == -1) {
  198.  
  199.         //status = CK_Get_last_error();
  200.         cout << "Stats Worker: received socket error on header send\n" << endl;
  201.         return -1;
  202.     }
  203.     cout << "Sending start boundary string " << endl << buffer2 << endl;
  204.     if (send(sock, buffer2, count1, 0) == -1) {
  205.  
  206.         //status = perror("send");
  207.         cout << "Stats Worker: received socket error %d on header send\n" << endl;
  208.         return -1;
  209.     }
  210.     cout << "Now Sending file " << Traveller_output_buffer << endl;
  211.     //Now send the flat file
  212.     if (send(sock, Traveller_output_buffer, data_len, 0) == -1) {
  213.  
  214.         //status = CK_Get_last_error();
  215.  
  216.         cout << "Stats Worker: received socket error %d on message send\n" << endl;
  217.         return -1;
  218.     }
  219.     cout << "Sending ending boundary string " << endl << buffer3 << endl;
  220.     if (send(sock, buffer3, count2, 0) == -1) {
  221.  
  222.         //status = perror("send");
  223.         cout << "Stats Worker: received socket error %d on header send\n" << endl;
  224.         return -1;
  225.     }
  226.     /*cout << "Sending Last part of the form data" <<  endl << buffer1+first_form_len << endl;
  227.  
  228.     //Send the last part of the form data to the HTTP server.
  229.     if (send(sock, buffer1+first_form_len, second_form_len, 0) == -1) {
  230.  
  231.         //status = CK_Get_last_error();
  232.         cout << "Stats Worker: received socket error %d on header send\n" << endl;
  233.         return -1;
  234.     }*/
  235.     /*else
  236.     {
  237.         cout << "Data has been Sent Successfully" << endl;
  238.     }*/
  239.  
  240.     //Receive server response
  241.     //cout << "Buffer = " << buf << endl;
  242.     n = recv(sock, buf, 2000, 0);
  243.     //cout << "Server Response is " << endl << buf << endl;
  244.     if(n < 0)
  245.     {
  246.         cout << "Upload Failed" << endl;
  247.         return -1;
  248.     }
  249.  
  250.     // Make sure this is a HTTP header
  251.     if (!strstr(buf, "HTTP")) {
  252.  
  253.         cout << "Stats Worker: response doesn't contain HTTP\n";
  254.         return -1;
  255.     }
  256.  
  257.     if (n < 0) {
  258.  
  259.         cout << "Stats Worker: response status is " << n;
  260.         return -1;
  261.     }
  262.  
  263.         // while (n > 0) {
  264.      //printf(buf);
  265.             if(!strstr(buf, Successful_Upload))
  266.          {
  267.         cout << "Traveller Upload Failed. Server Responded " << buf;
  268.         return -1;
  269.         }
  270.          else
  271.         {
  272.          cout << "Traveller Upload Successfull. \n Server Response is \n" << buf;
  273.          //return 0;
  274.         }
  275.             //n = recv(sock, buf, 10480, 0);
  276.          //}
  277.  
  278. //close(sock);
  279. }
  280.  
Appreciate any help.
Thanks
Sep 16 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I don't know C++. If you are using the correct enctype and the POST method and you are getting no error messages when running the script it is most likely a server issue. Try using a differnt directy to write the file to.
Sep 16 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
Are you uploading a file smaller than the post max setting?

$CGI::POST_MAX = 10240 * 5000;

5 megabytes in this case.
Sep 16 '08 #7

P: 8
Are you uploading a file smaller than the post max setting?

$CGI::POST_MAX = 10240 * 5000;

5 megabytes in this case.
Hi,
Thanks for replying. Yes I am uploading just a 2 megabytes file and it's always going to be this size.
Thanks,
Sep 16 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
try writing to a different directory.
Sep 16 '08 #9

P: 8
try writing to a different directory.
Hi Kevin,
Thanks for all the suggestions. I tried writing to a different directories however no success, still the uploaded file is blank.
I think maybe there is something wrong or missing in my C++ code.
I will try some C++ forums.
Though thanks for all your suggestions.
Thanks,
Mohit
Sep 17 '08 #10

eWish
Expert 100+
P: 971
Not sure if this will help, but you could look at an article that was written by a member on how to upload files. Are you trying to upload a text file or image?

You might compare your code.

Just a thought. Also, I suppose that your problem could be coming from the C++.

--Kevin
Sep 17 '08 #11

P: 8
Not sure if this will help, but you could look at an article that was written by a member on how to upload files. Are you trying to upload a text file or image?

You might compare your code.

Just a thought. Also, I suppose that your problem could be coming from the C++.

--Kevin
Hi Kevin,
Thanks for the reply. I am trying to upload a text file. I already read the article you have pointed to.
Still I am getting a blank file. It seems like the cgi script is not seeing the file data that I am sending through the socket.
Thanks,
Mohit
Sep 17 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.