gpt4 book ai didi

php - 无法在图像上传中将变量传递给PHP脚本

转载 作者:行者123 更新时间:2023-12-01 19:04:22 25 4
gpt4 key购买 nike

我有一个脚本,可以通过PHP成功将图像上传到服务器。上载部分有效,但我也尝试将变量传递到上载图像的PHP脚本。

我非常有信心我的PHP代码很好,我只是不知道Objective-C出了什么问题。我还注意到,我从不称呼NSData * ... = [NSData dataWithContentsOfURL:[NSURL URLWithString:...]];但我不知道那是问题所在...

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\r\n\r\n", [data
length] ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;
name=\"picture\"; filename=\"%@.png\"\r\n", @"newfile"]
dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];

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

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",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);

我的PHP脚本:
<?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!";
}
?>

最佳答案

在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\"\r\n\""];
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 stringWithString:string] 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*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *responseDictionary1;
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil];
}

关于php - 无法在图像上传中将变量传递给PHP脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com