Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:43:07+00:00 2026-05-18T05:43:07+00:00

I need a way to upload a file and POST it into php page

  • 0

I need a way to upload a file and POST it into php page

the php is on apache server

and i use this c code in my desktop application:

It’s a socket by c code

When I execute the Program , can get

The file size is 772906
open server ok
The file size is 772906
Connect to server ok
Open file ok
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 102400 bytes, Sending 102400 bytes
fread 56106 bytes, Sending 56106 bytes
Sending file Finished!
result: Return Code:
Upload:
Type:
Size: 0 Kb
Temp file:
Stored in:

But file is not exist .

I guess maybe it’s possible HTTP protocol ,But I don’t know how to do.

would you tell me details ?

Thanks a lot.

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <arpa/inet.h>





int main(){



    int sockfd, numbytes;

    FILE *fp;

    char buf[300];

    char filename[10]="1.jpg";

    char send1[100];

    struct sockaddr_in address;

    struct stat filestat;

    int  retval;

    char *str="GET /some address/upload_file.php?";



    //Get file stat

    if ( lstat(filename, &filestat) < 0){

        exit(1);

    }

    printf("The file size is %lu\n", filestat.st_size);



    //TCP socket

    if ( ( sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1 ){

        perror("socket");

        exit(1);

    }else{

    printf("open server ok\n");

    }



    #if 1

        //Initial, connect to port 80 

        address.sin_family = AF_INET;

        address.sin_port = htons(80);

        address.sin_addr.s_addr = inet_addr("xx.xx.xx.xx");

        bzero( &(address.sin_zero), 8 );

    #endif



    //Get file stat
    if ( lstat("1.jpg", &filestat) < 0){
        exit(1);
    }
    printf("The file size is %lu\n", filestat.st_size);
    fp = fopen("1.jpg", "rb");

    //Connect to server

    if ( connect(sockfd, (struct sockaddr*)&address, sizeof(struct sockaddr)) == -1){

        perror("connect");

        exit(1);

    }else{

    printf("Connect to server ok\n");

    }



    if ( (fp = fopen(filename,"rb")) == NULL){

            perror("fopen");

            exit(1);

    }else{

    printf("Open file ok\n");

    }   



    sprintf(send1,"%s\n",str);

    retval= send(sockfd,send1,sizeof(send1),0);





    //Sending file

    while(!feof(fp)){
        numbytes = fread(buf, sizeof(char), sizeof(buf), fp);
        //printf("fread %d bytes, ", numbytes);
        numbytes = write(sockfd, buf, numbytes);
        //printf("Sending %d bytes\n",numbytes);
    }

    printf("Sending file Finished!\n");



    //Receive from server

    if ( (numbytes = recv(sockfd, buf, sizeof(buf)+1,0) ) == -1 ){

            perror("recv");

            exit(1);

        }

    printf("result: %s\n\n\n", buf);



    fclose(fp);

    close(sockfd);

    return 0;

} 

my php is :

<?php





if (1){ 



if ($_FILES["file"]["error"] > 0){ 

        echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 

    }else{ 

         echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 

        echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 

        echo "Type: " . $_FILES["file"]["type"] . "<br />"; 

        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; 

        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 



        //move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
        move_uploaded_file($_FILES["file"]["tmp_name"], "5446");  

        echo "Stored in: " .  $_FILES["file"]["name"]; 



    } 



}else{ 

    echo "Invalid file"; 

} 

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:43:08+00:00Added an answer on May 18, 2026 at 5:43 am

    If you want to upload a file you should begin with POST /file/to/somewhere.php not GET.

    If you want to see the protocol, you could either read up on HTTP specs or even better, install wireshark and do the upload with a browser. Then you can see in wireshark which messages are sent by the browser and just replicate these with your program.

    IIRC the protocol should look like this:

    POST /to/somewhere.php HTTP/1.1
    Host: www.yourserver.com
    Content-Type: multipart/form-data;boundary=fdhskjfhldjkshfdksl----
    
    fdhskjfhldjkshfdksl----
    Content-Disposition: name=data;filename=foo.jpg
    Content-Type: image/jpg
    
    <binary data here>
    

    I’m typing this from my head so you really should watch the browser do it, but that’s the general idea of how post works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As it states, I need to upload a file, and then use it in
I have developed a download/upload manager script. When I upload a file via POST
I need to upload same file to 2 different place in same FTP. Is
I need to upload same file to 2 different place in same FTP. Is
I have a form which uploads multiple file fields into a db using codeigniter.
This is my scenario: I need to copy files to a sharepoint document library
I'm currently building a web-based file upload/download hub for a company that wanted an
I'm really confused on how to go about doing this. I want to be
I have been tasked to look after an ASP.Net WebForms application that communicates with
I struggled half a day and came to conclusion that it can't be done.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.