iProgrammer
iProgrammer

Reputation: 3107

Image is getting corrupted when posting to server from iPhone

Here is my code to upload image to php server but image is uploaded as corrupted image at server.Below is my code please tell me the issue..I have tried with cocoa rest client, image is uploading properly from it.

    NSString *filename = @"connect-blue.png";
    UIImage *image = [UIImage imageNamed:filename];
    NSMutableURLRequest * request= [[[NSMutableURLRequest alloc] init] autorelease];
    [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"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
   [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [postbody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *imagedata = UIImageJPEGRepresentation(image,1.0f);

    [postbody appendData:[NSData dataWithData:imagedata]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]  dataUsingEncoding:NSUTF8StringEncoding]];

Php code is here

if(!empty($_FILES)){
//
  $imgname=$_FILES['image']['name'];
         $a=pathinfo($imgname);

  if(!empty($a['extension'])){  
   $img_name=$a['filename'];
   $img_ext=$a['extension'];
 }

  else

  {

    $img_name='';

   $img_ext='';

   }

   $img_pre=mktime();

   $image1=$img_pre."_".$imgname;
  move_uploaded_file($_FILES['image']['tmp_name'],  "uploads/images/" .  $image1);

Upvotes: 1

Views: 444

Answers (2)

iProgrammer
iProgrammer

Reputation: 3107

I found the solution Image Uploading

Upvotes: 2

Bhavesh Lathigara
Bhavesh Lathigara

Reputation: 1415

You can manage the frame of image for both technology I mean when you send image from iphone , the size of image is still the same in php I think you manage Async Image class

Upvotes: 0

Related Questions