【发布时间】:2010-11-23 17:06:34
【问题描述】:
我需要一种上传文件并将其发布到 php 页面的方法
php 在 apache 服务器上
我在我的桌面应用程序中使用了这个 c 代码:
这是一个c代码的socket
当我执行程序时,可以得到
文件大小为 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 字节
发送文件完成!
结果:返回码:
上传:
类型:
大小:0 Kb
临时文件:
存储在:
但是文件不存在。
我猜可能是HTTP协议,但我不知道该怎么做。
你能告诉我细节吗?
非常感谢。
#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;
}
我的 php 是:
<?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";
}
?>
【问题讨论】:
标签: php c sockets file-upload