如何在PHP中获取或POST多部分数据请求

时间:2014-02-27 07:10:51

标签: php ios iphone web-services

我从iOS发送多部分请求以在服务器上上传图像或doc文件。 我的请求就像

    UIImage *im = [UIImage imageNamed:@"57X57.png"];
    NSData *imageData = UIImageJPEGRepresentation(im, 90);
    NSString *urlString = @"my URL/upload.php";


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSString *imgName = @"raj.png";
    NSLog(@"image name : %@",imgName);

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];
    NSLog(@"setRequest : %@", request);


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnstring is : %@",returnString);

并在PHP结束

<?php
    print_r($_FILES);
    print_r($_POST);
?>

当我写 print_r($ _ FILES)时,它会在iOS端给我输出

returnstring is : Array
(
    [files] => Array
        (
            [name] => raj.png
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpI93DO3
            [error] => 0
            [size] => 366784
        )
)
{"success":1}

但是在PHP服务器端,数组是空的,即Array()。

我是PHP的Web服务开发新手。请建议我哪里错了。 我对iOS端代码(即向服务器发送多部分请求)非常有信心,因为我使用相同的代码将多部分数据上传到基于java的Web服务(java中的RESTFull Web服务)但是我对PHP方面的知识不多。 请指导我如何在PHP端获取多部分请求。

1 个答案:

答案 0 :(得分:1)

嗨,请查看[link] Trouble Passing Variables to PHP Script on Image Upload我已经发布了这个问题的答案,PHP代码也在评论中

供您参考

PHP代码

$uploaddir = "../images/profile_images/";  
$file = basename($_FILES['image']['name']);  
$uploadfile = $uploaddir . $file;   
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))  
{  
    $updt_img = "UPDATE tiny_users SET profile_photo = '".$file."' WHERE user_id = '".$final_res[1]."'";  
    mysql_query($updt_img);  
}

要将多个图像上传到服务器,请使用以下代码

-(void)uploadMultiplePics  
{   
NSString *string ;   
NSData *imageData;   
NSString*urlString=[NSString stringWithFormat:@"http://******"];   
// urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@""];   
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];   
[request setURL:[NSURL URLWithString:urlString]];    
[request setHTTPMethod:@"POST"];   
NSMutableData *body;   
body = [NSMutableData data];  
for(int j=0;j < scrollViewImageArray.count;j++)   // scrollViewImageArray is images count    
{      
double my_time = [[NSDate date] timeIntervalSince1970];   
int k=j+1;    
NSString *imageName = [NSString stringWithFormat:@"%d%d",j,(int)(my_time)];   
NSString *imagetag=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image%d\"; filename=\"",k];   
string = [NSString stringWithFormat:@"%@%@%@", imagetag, imageName, @".jpg\"\r\n\""];  
NSString *boundary = @"---------------------------14737809831466499882746641449";   
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];   
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];    
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];   
UIImage *image=[[UIImage alloc]init];   
image=[scrollViewImageArray objectAtIndex:j];   
//  scrollViewImageArray images array   
imageData = UIImageJPEGRepresentation(image, 90);      
[body appendData:[NSData dataWithData:imageData]];   
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];     
}   
[request setHTTPBody:body];   
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];   
NSString*s=   [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];    

}

希望这会对你有所帮助。