0

I am sending multipart request from iOS to upload image or doc file on server. My request is like

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

and at PHP end

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

When I write print_r($_FILES) it will give me output at iOS end as

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

But at PHP server side the array is empty i.e Array().

I am new to web service development in PHP. Please suggest me where I am wrong. I am very confident about iOS side code (i.e sending multipart request to server) because I used the same code to upload multipart data to java based web service (RESTFull web service in java) But I not having much knowledge about PHP side. Please guide me how would I fetch multipart request at PHP end.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
Raj
  • 637
  • 13
  • 32
  • I don't understand the question, because what you're seeing in iOS is the result of the server's `print_r($_FILES);` and `print_r($_POST);`. So, why do you say that "at PHP server side the array is empty"? Your `print_r` just demonstrated that it's not empty at the server side. – Rob Feb 27 '14 at 07:46

1 Answers1

1

Hi please check this [link] Trouble Passing Variables to PHP Script on Image Upload I have posted an answer for this issue already PHP code is also there in comments

For your reference

PHP code

$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);  
}

To upload multiple images to server use below code

-(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];    

}

Hope this will help you.

Community
  • 1
  • 1
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
  • And also I want to ask you one thing Can we upload multiple images in one request? – Raj Feb 27 '14 at 07:46
  • @Raj Yes, just add additional `Content-Disposition: form-data` sections for each of the images. And by the way, this code snippet is obviously looking for a `name` of `image`, but you're using `files`. Make sure the name you use in your client code matches the name you use in your server code. – Rob Feb 27 '14 at 07:49
  • ok I'll definitely check for multiple image upload and get back to you :) – Raj Feb 27 '14 at 08:07
  • 1
    @Raj see my answer for posting or uploading multiple images to server – Charan Giri Feb 27 '14 at 08:25
  • yes I am using the same code for uploading image from iOS , but is there any other setting required at PHP end to read request incoming request. – Raj Feb 27 '14 at 08:34
  • in your web service url it will be better if they add count tag... other than this nothing is needed to change. if they add count based on it they can insert images in DB folder – Charan Giri Feb 27 '14 at 08:53