Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image to the PHP server from iOS

Tags:

I know this question has been asked earlier as well but my issue is a bit different.

I want to upload an image to the PHP server and i want to send more parameters along with an image from an iOS. I searched on the google and found two solutions:

  1. Either we will send an image as Base64 encoded string in JSON. Referred link.

  2. Or we will upload an image to server using form data. I have referred this link. If someone refers me this way, then please help me to add more parameters in this API.

Now my question is, which one is the best way to upload an image to the server and i have to send more parameters (username, password and more details) in the same web service call.

Thanks in advance.

like image 478
Vinay Jain Avatar asked Dec 22 '13 10:12

Vinay Jain


People also ask

How can I upload an image from a URL in PHP?

In PHP script for check url is valid or not, we have use filter_var() function. If url is proper then by using file_get_contents() function reads a image file into a string. And lastly by using file_get_contents() function, we have write image string to a file.

Can I upload images to phpmyadmin?

Check whether the user selects an image file to upload. Retrieve the content of image file by the tmp_name . Create the connection to MySQL database and select the database. Insert the binary content of the image in the images table.


1 Answers

You can upload image from iOS App to PHP server like this two way:

Using New AFNetworking :

#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"

    NSString *stringUrl =@"http://www.myserverurl.com/file/uloaddetails.php?"
    NSString *string =@"http://myimageurkstrn.com/img/myimage.png"       
    NSURL *filePath = [NSURL fileURLWithPath:string];

   NSDictionary *parameters  = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         [formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image 
     }
     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"%@",[responseObject valueForKey:@"Root"]);
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];     

     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];

     }];

Second use NSURLConnection:

-(void)uploadImage
    {       
        NSData *imageData = UIImagePNGRepresentation(yourImage);

        NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------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 stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"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];

        [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    }

This both way working fine for uploading image from app to php server hope this helps for you.

like image 196
Nitin Gohel Avatar answered Sep 30 '22 20:09

Nitin Gohel