上传和POST文件到PHP页面 [英] Upload and POST file to PHP page

查看:202
本文介绍了上传和POST文件到PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来上传文件并将它POST到PHP页面

在PHP是Apache服务器上

和我使用这款C code在我的桌面应用程序:

这是用C code插座

当我执行该程序,就可以得到

的文件大小为772906
开放式服务器确定
文件大小为772906
连接到服务器确定
打开文件确定
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 102400字节,发送字节102400
FREAD 56106字节,发送56106字节
发送文件完成!
结果:返回code:
上传:
类型:
大小:0 KB
临时文件:
储存在:

但是文件是不存在的。

我想也许这是可能的HTTP协议,但我不知道该怎么办。

你能告诉我细节?

非常感谢。

 的#include<&stdio.h中GT;#包括LT&;&stdlib.h中GT;#包括LT&;&errno.h中GT;#包括LT&;&string.h中GT;#包括LT&;&unistd.h中GT;#包括LT&;&pthreads.h中GT;#包括LT&; netinet / in.h中>#包括LT&; SYS / socket.h中>#包括LT&; SYS / wait.h>#包括LT&; SYS / types.h中>#包括LT&; SYS / stat.h>#包括LT&; ARPA / inet.h>诠释主(){    INT的sockfd,的numBytes;    FILE * FP;    焦炭BUF [300];    字符文件名[10] =1.JPG;    焦炭SEND1 [100];    结构sockaddr_in的地址;    结构统计FILESTAT;    INT RETVAL;    字符*海峡=GET /一些地址/ upload_file.php?    //获取文件统计    如果(LSTAT(文件名,和放大器; FILESTAT)小于0){        出口(1);    }    的printf(文件大小为%lu \\ n,filestat.st_size);    // TCP套接字    如果((的sockfd =插座(AF_INET,SOCK_STREAM,0))== -1){        PERROR(插座);        出口(1);    }其他{    的printf(开放式服务器OK \\ n);    }    1#如果        //初始,连接到端口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);    #万一    //获取文件统计
    如果(LSTAT(1.JPG,&放大器; FILESTAT)小于0){
        出口(1);
    }
    的printf(文件大小为%lu \\ n,filestat.st_size);
    FP = FOPEN(1.JPG,RB);    //连接到服务器    如果(连接(的sockfd,(结构sockaddr *)及地址的sizeof(结构sockaddr))== -1){        PERROR(连接);        出口(1);    }其他{    的printf(连接到服务器OK \\ n);    }    如果((FP = FOPEN(文件名,RB))== NULL){            PERROR(FOPEN);            出口(1);    }其他{    的printf(打开文件就OK \\ n);    }    sprintf的(SEND1%s \\ n,STR);    RETVAL =送(的sockfd,SEND1,sizeof的(SEND1),0);    //发送文件    而(!的feof(FP)){
        的numBytes = FREAD(BUF,sizeof的(炭)的sizeof(BUF),FP);
        //输出(FREAD%d字节,的numBytes);
        的numBytes =写(的sockfd,BUF,的numBytes);
        //的printf(发送%d字节的\\ n,的numBytes);
    }    的printf(发送文件后\\ N!);    //从服务器接收    如果((的numBytes =的recv(的sockfd,buf中,sizeof的(BUF)+ 1,0))== -1){            PERROR(RECV);            出口(1);        }    的printf(结果:%S \\ n \\ n \\ n,BUF);    FCLOSE(FP);    接近(的sockfd);    返回0;}

我的PHP是:

 < PHP如果(1){如果($ _FILES [文件] [错误] 0){        回声返回code:。 $ _FILES [文件] [错误。 < BR />中;    }其他{         回声返回code:。 $ _FILES [文件] [错误。 < BR />中;        回声上传。 $ _FILES [文件] [名。 < BR />中;        回声类型:。 $ _FILES [文件] [型。 < BR />中;        回声大小。 ($ _FILES [文件] [大小] / 1024)。 KB< BR />中;        回声临时文件。 $ _FILES [文件] [tmp_name的值。 < BR />中;        // move_uploaded_file($ _ FILES [文件] [tmp_name的值],$ _FILES [文件] [名称]);
        move_uploaded_file($ _ FILES [文件] [tmp_name的值],5446);        回声存储位置:。 $ _FILES [文件] [名称];    }}其他{    回声无效的文件;}?>


解决方案

如果你想上传你应该用POST开始/file/to/somewhere.php无法获取文件。<​​/ P>

如果你想看到的协议,您既可以阅读了关于HTTP规范,甚至更好,Wireshark的安装和使用浏览器做上传。然后,你可以在Wireshark中看到哪些消息是由浏览器发送的,只是复制这些与你的程序。

IIRC协议应该是这样的:

 张贴/to/somewhere.php HTTP / 1.1
主持人:www.yourserver.com
内容类型:多重/ form-data的;边界= fdhskjfhldjkshfdksl ----fdhskjfhldjkshfdksl ----
内容处置:名称=数据;文件名= foo.jpg
内容类型:图像/ JPG&LT;此处&GT的二进制数据;

我是从我的头打字这个让你真正应该关注的浏览器去做,但这是后如何工作的总体思路。

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"; 

} 

?>

解决方案

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.

这篇关于上传和POST文件到PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆