473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Uploading file to a server

8 New Member
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
11 2248
numberwhun
3,509 Recognized Expert Moderator Specialist
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
starter08
8 New Member
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
4,059 Recognized Expert Specialist
what is the enctype of the <form> tag?
Sep 16 '08 #4
starter08
8 New Member
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
4,059 Recognized Expert Specialist
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
4,059 Recognized Expert Specialist
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
starter08
8 New Member
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
4,059 Recognized Expert Specialist
try writing to a different directory.
Sep 16 '08 #9
starter08
8 New Member
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
971 Recognized Expert Contributor
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
starter08
8 New Member
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

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

Similar topics

9
2365
by: R. Rajesh Jeba Anbiah | last post by:
Q: How should I handle file upload? A: File uploading requires HTML form of content type "multipart/form-data". The file content has to be POSTed/submitted via the form and once the file is...
5
7815
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
1
2189
by: Jonathan | last post by:
Hi everyone, I have a problem with the file uploading in Asp.Net and I have read a lot on forums on this but never found an answer. Here is the problem: I know Asp.Net maximum Length for...
13
4281
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
3
2043
by: Dean Richardson | last post by:
Hi, I'm having trouble uploading files via a PHP script. Whenever I upload a file greater than 10K, the file gets corrupted. However, text files upload OK. When I check the FTP Server log I...
3
1900
ganesanji
by: ganesanji | last post by:
hi all, I have written a php coding for uploading a file to a specific folder or location in server which is a Linux server. I think the coding for file uploaing is correct. But it does not...
221
366960
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
2699
by: LoriFranklin | last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form. I am using some code that I found from Jacob at...
0
1900
by: najmi | last post by:
hai. i have one problem that is to upload file..it working perfectly in my computer but fail when deploy at server..the system is to browse the file,then system will zip it before upload it to the...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.