gpt4 book ai didi

php - 将 base64 编码图像上传到 Mamp 服务器

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

我正在将 base64 编码格式的图像发送到服务器
使用以下方法

//用于上传图片......................

- (void)ImageUpload:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
NSString *urlString=@"http://192.168.77.145/uploadImage.php";

NSURL *url = [NSURL URLWithString:urlString];
NSString *base64ImageString = [imageData base64EncodedStringWithOptions:kNilOptions]; // iOS 7+

base64ImageString = [base64ImageString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSString *jsonReqString = [[NSString alloc] initWithFormat:@"{\"photo\":\"%@\",\"imageExtension\":\"%@\"}", base64ImageString,@"123"];


NSString *post =[[NSString alloc] initWithFormat:@"request=%@",jsonReqString];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!data)
{
NSLog(@"wifi error");
NSLog(@"sendAsynchronousRequest error: %@", connectionError);

return ;
}
else
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

}
}

}];
}

但我没有得到base64字符串到服务器。
我的参数照片和图像扩展都为空。

这是我的服务器端代码
  <?php

$base = $_POST['photo'];
echo $base;
$filename = $_POST['imageExtension'];
echo $filename;
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');

$file = fopen('uploadedimages/'.$filename, 'wb');

// Create File
fwrite($file, $binary);
fclose($file);

echo 'Image upload complete, Please check your php file directory';
?>

感谢帮助

最佳答案

问题出在服务器端的第二行

您正在尝试获得关键“照片”的值(value)

$base = $_POST['photo'];

你需要改变那条线
  $base = $_POST['request'];

之后,您需要添加以下代码
$baseImg = json_decode($base, true);
$filename = $baseImg["imageExtension"];
$tmpImg = $baseImg["photo"];

$binary=base64_decode($tmpImg);

关于php - 将 base64 编码图像上传到 Mamp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29410824/

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