Is there a file size limit in uploading files with AFNetWorking?

85 Views Asked by At

I am using AFNetworking 3.0 to upload some pictures to a server in php. The code is very simple. But It doesn't work, I don't know the reason.

I always get the php error :

UPLOAD_ERR_INI_SIZE.

The limit size in php.ini is 2M, my picture size is only 200K, I tried using safari to test , it worked well.

When I used a very small picture, 6k, it worked too.

Here is my code:


client:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //   manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];

    [manager POST:@"http://192.168.1.108/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

        UIImage *image =[UIImage imageNamed:@"moon"];
        NSData *data = UIImagePNGRepresentation(image);

        //file name
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];


        [formData appendPartWithFileData:data name:@"img" fileName:fileName mimeType:@"image/jpeg"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {


    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *result  =[[ NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@",result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"fail %@", error);
    }];
}

upload.php

<?php
$file = $_FILES['img'];
if ($file['error'] == 0) {
    $typeArr = explode("/", $file["type"]);
    if($typeArr[0]== "image"){
        $imgType = array("png","jpg","jpeg");
        if(in_array($typeArr[1], $imgType)){ 
            $imgname = "file/".time().".".$typeArr[1];
            $bol = move_uploaded_file($file["tmp_name"], $imgname);
            if($bol){
                echo "ok";
            } else {
                echo "fail";
            };
        };
    } 
} else {
    echo $file['error'];
};

Any help? Thanks.

1

There are 1 best solutions below

1
xptrip On

I just found out what's wrong.

My picture is jpg , I used UIImagePNGRepresentation to get data, I should use UIImageJPEGRepresentation instead.

Thanks.