php - 图片上传到 SSL 文件夹,状态代码 200,标题 {connection = closed}

标签 php ios ssl

我正在尝试连接到服务器上我的 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 请求中发送字段 commentarioid

您必须包含这些字段或修改服务器端 php 代码。

由于缺少字段,您可能还想在发送错误响应时返回另一个 HTTP 状态代码。我建议“错误请求”为 400。

关于php - 图片上传到 SSL 文件夹,状态代码 200,标题 {connection = closed},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30890790/

相关文章:

ios - UITabBar 图片不清晰

ios - UIWebView 无需转到 iOS 的视频播放器即可播放 Html 5 视频

php - 如何将 php session 绑定(bind)到用户 SSL 证书

c# - 同时为自托管 wcf 服务和 IIS 使用端口 443

php - Codeigniter,来自数据库的动态菜单

php - CodeIgniter:删除 WAMP 上 URL 中的 "index.php"

php - 无法搜索短名称的名称

iOS Autolayout : two buttons of equal width,并排

php - Mysql 使用具有二进制数据类型的 where 子句

node.js - 在本地安装 npm 时忽略 ssl signage 让 bower 恢复包