0

I need to upload picture from an iphone apps to a server.

I use this code to try on localhost :

  NSData *storeData = UIImageJPEGRepresentation(pictureItem, 90);
                NSString *URLString = @"http://localhost:8080/image_item/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"];

                NSMutableData *body = [NSMutableData data];
                [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%d.jpg\"\r\n", idItem] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[NSData dataWithData:storeData]];
                [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [request setHTTPBody:body];

                NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                NSLog(@"%@",returnString);

with image_item my folder to stock image and idItem the name of the picture.

I call a upload.php page :

   <?php
$uploaddir = './';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$newName = $uploadDir . $uploadFile;


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "Temp file uploaded. \r\n";
} else {
    echo "Temp file not uploaded. \r\n";
}

if ($_FILES['userfile']['size']> 300000) {
    exit("Your file is too large."); 
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
    $postsize = ini_get('post_max_size');   //Not necessary, I was using these
    $canupload = ini_get('file_uploads');    //server variables to see what was 
    $tempdir = ini_get('upload_tmp_dir');   //going wrong.
    $maxsize = ini_get('upload_max_filesize');
}
?>

But the probleme is the following : there is my picture on the folder with the good name but i can't open the picture. There is the following error : It may be damaged or use a file format that Preview doesn’t recognize.

So I don't understand... Can anyone help me?

Thanks.

Jonathan V
  • 21
  • 3
  • Where are you seeing the error that it may be damaged? When you hit it in a browser? Is the filesize the same before/after? Can you do md5sum on it before/after? Everything generally looks correct. – Matt Feb 21 '13 at 04:38
  • How about the filesize? Are there any errors in the php log? – Matt Feb 21 '13 at 04:46
  • I see this error when i want to open the picture with preview on mac OS. And when I hit in the browser there is nothing too. For the size, the picture on the server is 5 time as big as the initial picture.. – Jonathan V Feb 21 '13 at 04:52
  • No nothing on the php log – Jonathan V Feb 21 '13 at 04:53
  • Is the filesize the same before/after? – Matt Feb 21 '13 at 04:57
  • No.. the picture's size on the server is 5 time as big as the initial picture's size – Jonathan V Feb 21 '13 at 05:02
  • Did you see this? http://stackoverflow.com/questions/11413846/uploading-an-image-from-ios-to-php?rq=1 – Matt Feb 21 '13 at 05:06
  • Oh thank you very much. I saw with this topic I forgot one line in my code for the body. Thank you so much mkaatman ! – Jonathan V Feb 21 '13 at 05:24

1 Answers1

2

I find the solution. A line is missing in the body. The right code is here :

NSString *boundary = @"---------------------------14737809831466499882746641449";
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
            [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

            NSMutableData *body = [NSMutableData data];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%d.jpg\"\r\n", idItem] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:storeData]];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [request setHTTPBody:body];

            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
suMi
  • 1,536
  • 1
  • 17
  • 30
Jonathan V
  • 21
  • 3