在服务器上上传多张图片少一幅

时间:2018-10-01 21:50:47

标签: php ios objective-c image upload

当我尝试从阵列上传多个图像(例如3张图像)时,在服务器上仅上传2张。如果我上传2张,则得到1张,如果是1张,则根本没有任何图像上传。我的代码有什么问题?

_arrImage数组中我可以得到正确数量的图像。

这是客观的c:

NSURL *url = [NSURL URLWithString:@"http://www.example.com/upload.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"unique-consistent-string";

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *dataForm = [NSMutableData data];

    if([_arrImage count] > 0) {
        int i = 0;
        for (NSDictionary *imageDic in _arrImage) {
            UIImage *myimage = [imageDic objectForKey:@"mainImage"];
            NSData *imageData = UIImageJPEGRepresentation(myimage, 0.8);
            [dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [dataForm appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileToUpload[]\"; filename=\"%d.jpg\"\r\n",  i] dataUsingEncoding:NSUTF8StringEncoding]];
            [dataForm appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            [dataForm appendData:[NSData dataWithData:imageData]];
            i++;
        }
    }

    [request setHTTPBody:dataForm];

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {

        if(data.length > 0)
        {
            NSLog(@"response: %@", response);
        }else{
            NSLog(@"%@", [error localizedDescription]);
        }

    }] resume];

而php是:

<?php  
    $uploadFolder = "users/posts/";
    if (!file_exists($uploadFolder)) {
        mkdir($uploadFolder);
    }

    if (is_array($_FILES["fileToUpload"])) {
        $numberOfFiles = count($_FILES["fileToUpload"]["name"]);
        for ($i = 0; $i < $numberOfFiles; $i++) { 
            $uploadFile = $uploadFolder . "/" . basename($_FILES["fileToUpload"]["name"][$i]);
            $imageFileType = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));

            if (!(getimagesize($_FILES["fileToUpload"]["tmp_name"][$i]) !== false)) {
                echo "Sorry, your image is invalid";
                exit;
            }

            if ($_FILES["fileToUpload"]["size"][$i] > 10000000) {
                echo "Sorry, your file is too large.";
                exit;
            }

            if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                exit;
            }

            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $uploadFile)) {
                echo "Upload image ".basename($_FILES["fileToUpload"]["name"][$i])." successfully!";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }
?>

所以您可以帮助解决问题。

如果我上传2张图片,则输出$ _FILES:

$_FILES = array (
  'fileToUpload' => 
  array (
    'name' => 
    array (
      0 => '0.jpg',
      1 => '1.jpg',
    ),
    'type' => 
    array (
      0 => 'image/jpeg',
      1 => '',
    ),
    'tmp_name' => 
    array (
      0 => '/tmp/phpknUF0r',
      1 => '',
    ),
    'error' => 
    array (
      0 => 0,
      1 => 3,
    ),
    'size' => 
    array (
      0 => 39270,
      1 => 0,
    ),
  ),
);

1 个答案:

答案 0 :(得分:0)

在您的Objective-C代码中添加

        [dataForm appendData:[NSData dataWithData:imageData]];
        i++;
    }

    [dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

}
for循环后的

。它将关闭传输的表单数据。