Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

ios - Trouble Passing Variables to PHP Script on Image Upload

I have a script that successfully uploads an image to my server via PHP. THe upload part works, but I am also trying to pass variables to the PHP script that is uploading the image.

I am pretty confident that my PHP code is just fine, I just dont know whats going wrong with the Objective-C. I also notice that I never call NSData *... = [NSData dataWithContentsOfURL:[NSURL URLWithString:...]]; But I dont know if thats the problem...

NSData *data = UIImageJPEGRepresentation(twitterImage, 90);

NSString *profileId = [[NSUserDefaults standardUserDefaults] stringForKey:@"passedId"];
NSString *userId = [[NSUserDefaults standardUserDefaults] stringForKey:@"userId"];
NSString *urlString = [NSString 
stringWithFormat:@"http://www.website.com/user_photo_upload.php?
member_id=%@&profile_id=%@",userId, profileId];

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

    NSString *boundary = @"_783465873689273842734";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; 
    boundary=%@",boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
    forHTTPHeaderField:@"Accept"];
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) 
    AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" 
    forHTTPHeaderField:@"User-Agent"];
    [request setValue:@"http://google.com" forHTTPHeaderField:@"Origin"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"Content-Length %d

", [data 
    length] ] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] 
    dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; 
    name="picture"; filename="%@.png"
", @"newfile"] 
    dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream

" 
    dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[NSData dataWithData:data]];

    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] 
    dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%d", [body length]] 
    forHTTPHeaderField:@"Content-Length"];

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

My PHP Script:

<?php

    $profile_id = $_GET['profile_id'];
    $member_id = $_GET['member_id'];

    $new_file_name="$profile_id-$member_id.png";

    $target_path = "./user_photos/";  

    $target_path = $target_path . $new_file_name;  

    if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {  
    echo "The file ".  basename( $_FILES['picture']['name'])." has been uploaded";  
    } else{  
    echo "There was an error uploading the file, please try again!";  
    } 
 ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this in Xcode

-(void)postMethod   
{   
 NSString *urlString = [NSString stringWithFormat:@"http:********web-services/register_user.php?firstname=%@&lastname=%@&email=%@&password=%@&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password];   

 UIImage *image=details.pic;
NSData *imageData =UIImageJPEGRepresentation(image, 0.1);
double my_time = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)];
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name="profile_pic"; filename="", imageName, @".jpg"
""];
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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString*s11=   [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *responseDictionary1;
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...