我正在尝试连接到服务器上我的 SSL 文件夹中的 php 文件以上传在我的应用程序中拍摄的图像,所有证书等都已到位并且目录的权限设置为 777 但我收到错误代码200 并且没有上传图片。
我的 ViewController.m 中与此相关的代码部分是
- (IBAction)subirImagen:(id)sender {
NSData *imageData = UIImagePNGRepresentation(self.imagen.image);
NSString *urlString = @"https://secure101.prositehosting.co.uk/php-upload-file-server-master2/subir.php";
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 initMultipart];
[body addPart:@"myFile" withFileName:@"imagen.png" withNSData:imageData];
[body addPart:@"myName" withValue:@"Ricardo"];
[body writeLastBoundary];
[request setHTTPBody:body];
NSURLResponse* response = nil;
NSError* err = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSHTTPURLResponse *HTTPURLResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPURLResponse statusCode];
NSString* mensaje = [NSString stringWithFormat:@"Request: %ld",(long)statusCode];
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Server"
message:mensaje
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
NSLog(@"Response Meta: %@", response);
NSLog(@"String sent from server %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
//As was advised
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
//crash here
NSLog(@"%ld", (long)newResp.statusCode);
}
php文件是
<?php
header('Content-Type: application/json; charset=utf-8');
//header('Content-Type: text/html; charset=utf-8');
if (isset($_POST['comentario']) && isset($_POST['id'])) {
$comentario = $_POST['comentario'];
$id = $_POST['id'];
if (isset($_FILES['myFile'])) {
move_uploaded_file($_FILES['myFile']['tmp_name'], "./" . $_FILES['myFile']['name']);
}
$respuesta = array("code"=>2000,"message"=>"successful", "comentario"=>$comentario, "id"=>$id);
// $class_response = json_decode(json_encode($respuesta));
// echo $class_response->comentario;
//echo $comentario;
echo json_encode($respuesta);
}
else{
$respuesta = array("code"=>4000,"message"=>"fail");
echo json_encode($respuesta);
}
?>
错误响应是
<NSHTTPURLResponse: 0x1545b4360> { URL: https://secure101.prositehosting.co.uk/php-upload-file-server-master2/subir.php } { status code: 200, headers {
Connection = close;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 17 Jun 2015 10:55:46 GMT";
Server = Apache;
"Transfer-Encoding" = Identity;
} }
2015-06-17 11:55:55.556 CameraApp[1647:643453] String sent from server {"code":4000,"message":"fail"}
我已经在 Xcode 的 info.plist 文件中实现了 TLS key ,以符合为 iOS 9 宣布的新标准,但仍然无法上传任何图像。
非常感谢任何帮助,谢谢。
最佳答案
请注意,200 是“OK”的 HTTP 状态代码。成功处理请求时返回。
因为您正在接收 JSON 响应
{"code":4000,"message":"fail"}
可以推断出条件语句
if (isset($_POST['comentario']) && isset($_POST['id']))
返回错误。
这并不奇怪,因为您没有在 POST
请求中发送字段 commentario
和 id
。
您必须包含这些字段或修改服务器端 php 代码。
由于缺少字段,您可能还想在发送错误响应时返回另一个 HTTP 状态代码。我建议“错误请求”为 400。
关于php - 图片上传到 SSL 文件夹,状态代码 200,标题 {connection = closed},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30890790/