4

I am trying to send image to my web site depending on user choice, every time the app should give the image's name which has been chosen by the user.

I am using Xcode 6 and the code below:

 NSString *urlString = @"http://MyWebSite.com/sendImages.php";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    // file
    NSData *imageData = UIImageJPEGRepresentation(self.imageView.image,90);
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.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:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // set request body
    [request setHTTPBody:body];
    //return and test
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:returnString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];

And her is my PHP file:

<? 
    $myparam = $_FILES['userfile']; //getting image Here //getting textLabe Here 
    $target_path = "khadamati/";
    if(move_uploaded_file($myparam['tmp_name'], $target_path . basename( $myparam['name'])))

    { echo "The file ". basename( $myparam['name']). " has been uploaded"; } 

    else 

    { echo "There was an error uploading the file, please try again!"; } 

    ?>

How can I change the myImage name in this line:

[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

I tried to find any thin that would help me, but I could not, so please find the mistake in my booth code.

1
  • You find your solution ? or any doubt ? Commented Oct 15, 2014 at 12:42

1 Answer 1

4

You should try this ,

uname,datestring ( i used here to define the user who has uploaded and the time when he uploaded ).

[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@receipt%@.jpg\"\r\n", uname,datestring]] dataUsingEncoding:NSUTF8StringEncoding]];

i hope it will be helpful to you , uname and datestring both are strings !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.