3

这是我的苗条框架 php 代码

    $app->post('/uploadPicture', function () {

    if (!empty($_FILES)) {

        global $config;
        $picturePath = $config->get('db', 'picture');

        $allowedExts = array("jpg", "jpeg", "png");
        $extension = end(explode(".", $_FILES["file"]["name"]));

        if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2500000 && in_array($extension, $allowedExts)) {

            if ($_FILES["file"]["error"] > 0) {

                echo "Error: " . $_FILES["file"]["error"] . "<br />";
            } else {

                if (move_uploaded_file($_FILES['file']['tmp_name'], $picturePath . $_FILES["file"]["name"])) {
                    echo "success";
                }

            }
        } else {

            echo "Invalid file type";
        }
    } else {

        echo "no file to upload";
    }

});

这就是我在iphone端使用的。

    -(void)uploadImage:(UIImage *)image withPersistentID:(NSString *)persistentID {

    [[MSMAMobileAPIClient sharedClient] POST:@"uploadPicture" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {

        NSData *imageData = UIImageJPEGRepresentation(image, 90);

        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", persistentID];

        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpg"];

    } success:^(NSURLSessionDataTask * task, id responderData) {

        self.itemImageBlock(YES);

    } failure:^(NSURLSessionDataTask * task, NSError * error) {

        NSLog(@"%@",[error.userInfo objectForKey:@"JSONResponseSerializerWithDataKey"]);
        self.itemImageBlock(NO);

    }];

}

在 iphone 端,它似乎就像服务器很忙一样挂起,然后它最终因超时而失败。

我一直在使用 CocoaRestClient 并且能够上传图像。

尝试从 iphone 上传时,我还看到文件被添加到 php 的临时目录中。

难道我做错了什么?

编辑:我刚刚在uploadPicture 函数中添加了一个错误日志行,它甚至看起来都没有被iphone 调用!:(

EDIT2:这是返回错误消息的 NSLog

Error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x17d6e1c0 {NSErrorFailingURLStringKey=https://192.168.1.15/mamobile/index.php/uploadPicture, NSErrorFailingURLKey=https://192.168.1.15/mamobile/index.php/uploadPicture, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x17db1460 "The request timed out."}

EDIT3:我消除了苗条的php框架,只是创建了一个只上传图片的简单php脚本。

<?php 


if (!empty($_FILES)) {

    //load configuration file
    require_once 'Lite/Lite.php';
    $config = new Config_Lite('config.ini');
    $picturePath = $config->get('db', 'picture');

        $allowedExts = array("jpg", "jpeg", "png");
        $extension = end(explode(".", $_FILES["file"]["name"]));

        if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2500000 && in_array($extension, $allowedExts)) {

            if ($_FILES["file"]["error"] > 0) {

                echo "Error: " . $_FILES["file"]["error"] . "<br />";
            } else {

                if (move_uploaded_file($_FILES['file']['tmp_name'], $picturePath . $_FILES["file"]["name"])) {
                    echo "success";
                }

            }
        } else {

            echo "Invalid file type";
        }
    } else {

        echo "no file to upload";
    }

?>

然后我改变了我的代码以这种方式工作。

NSString *urlString = [NSString stringWithFormat:@"https://%@/mamobile/uploadPicture.php", [[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]];
    AFHTTPSessionManager *imageSession = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
    imageSession.responseSerializer = [MSJSONResponseSerializerWithData serializer];
    [imageSession.requestSerializer setAuthorizationHeaderFieldWithUsername:@"fake username" password:@"fake password"];
    [imageSession.securityPolicy setAllowInvalidCertificates:YES];

    [imageSession POST:@"" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {

        NSData *imageData = UIImageJPEGRepresentation(image, 90);

        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", persistentID];

        NSLog(@"uploading image '%@' with size = %@",fileName,[NSByteCountFormatter stringFromByteCount:imageData.length countStyle:NSByteCountFormatterCountStyleFile]);

        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpg"];

    } success:^(NSURLSessionDataTask * task, id responderData) {
        NSLog(@"Success: %@", responderData);
        self.itemImageBlock(YES);

    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        NSLog(@"Error: %@",error);
        self.itemImageBlock(NO);

    }];

但我仍然收到相同的超时消息。所以它与slim php框架无关。

4

2 回答 2

0

https://github.com/AFNetworking/AFNetworking/issues/1510#issuecomment-29687300

有一个问题我也类似。解决方案是从以下代码中添加。

[formData appendPartWithFormData:[[NSNumber numberWithInt:imageData.length].stringValue dataUsingEncoding:NSUTF8StringEncoding] name:@"filelength"];
于 2013-12-03T06:54:11.597 回答
0

您得到的错误是:The request timed out.

这意味着,您从移动设备上传到 PHP 脚本的图像所花费的时间超过了 PHP 上脚本执行时间配置的最大设置。

要解决此问题,您可以在 PHP 脚本的开头添加:

<?php 

// Allow script to run longer
set_time_limit(600); // in seconds, set it to 0 to run forever until completion

// Proceed with upload
if (!empty($_FILES)) {
    ...
}
于 2013-10-23T09:19:28.837 回答