abdulheru abdulheru - 1 year ago 99
PHP Question

Uploading Images using ios to php

The images I try to upload from my Iphone gallery are all saved as


I want the exact image name which I am selecting from the gallery
and I want to create a folder (with the login username) and I want to store inside the folder

For example

  • Username : Mike

  • Image : welcome.jpg

Then I want to create a folder named
and save the image inside.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

[self dismissModalViewControllerAnimated:true];

img = [info valueForKey:UIImagePickerControllerOriginalImage];
//images.image = img;
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSString *urlString = @"";

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:[@"Content-Disposition: form-data; name=\"file\"; filename=\"ipodfile.jpg\"\r\n" 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];

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


This is the server-side code I'm using


echo "Upload: " . $oname . "<br />";
echo "Temp file: " . $tempname . "<br />";

echo "<br>";

echo "File Already exist";

echo "Sucessfully Stored in: " . $path. $oname;
echo "Not stored Sucessfully : " . $path.$oname;

Answer Source

You should take unique file name for every image like timestamp which will always be unique.

in below line you are setting image name so set it with time stamp.

 [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

You can kepp unique name aomething like this which i have used once,

            NSDate *currentDate = [[NSDate alloc] init];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
            NSString *localDateString = [dateFormatter stringFromDate:currentDate];
            NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
            NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",i];
            NSString *finalUniqueImageNAme = [cleanedString2 stringByAppendingString:@".jpg"];

Hope this will help ;)

Update :

you can use unique name like this,

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

And second thing you should use AFNetworking to do this kind of stuff. it'll manage all this kind of stuff like set boundary etc. Click here for AFNetworking github

Update 2 (according to query of comment):

You should use NSURLSession instead of nsurlconnection because connection is deprecated now so your code should be like this

 NSURLSession *session = [NSURLSession sharedSession];

[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    if (data) {

        //if response is in string format
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        //if response in json format then
        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];