iOS:无法通过restful api上传图片

时间:2015-08-19 00:52:47

标签: php ios image-uploading afnetworking-2

我正在努力通过PHP脚本将图像从ios上传到Web服务器。我已经用html表单测试了php脚本,它运行得很好,但是从手机发送时遇到了很多麻烦。

我目前正在使用AFNetworking 2.0并将其设置如下:

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://myexamplesite.com"]];
    AFHTTPRequestOperation *op = [manager POST:@"mobile_app/upload.php" parameters:nil
                     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:_worldmoonproject.imageData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];

在NSLog中它只是吐出以下内容:

  

2015-08-18 17:45:37.378 WMP [3265:280005]成功:*****(null)

这是我的服务器代码:

if(!empty($_POST))
{
    //The directory to save the photos
    define('MB', 1048576);
    $errors = array();
    $target_dir = "app_data/images/";
    $file_name = md5(date('Y-m-d H:i:s:u'));
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $upload_valid = false;
    $file_type = pathinfo($target_file, PATHINFO_EXTENSION);

    //Verify the file is an image
    $check = getimagesize($_FILES["file"]["tmp_name"]);

    if ($check !== false) 
    {
        $upload_valid = true;
    }
    else
    {
        $upload_valid = false;
        $errors[] = "FILE_NOT_IMAGE";
    }

    //Check if the file already exists
    if (file_exists($target_file))
    {
        $upload_valid = false;
        $errors[] = "FILE_ALREADY_UPLOADED";
    }

    //Verify the file is not too large
    if ($_FILES["file"]["size"] > 500*MB)
    {
        //the file is too large, display an error
        $upload_valid = false;
        $errors[] = "FILE_TOO_LARGE";
    }

    //Only allow image file types
    if ($file_type != "jpg" && $file_type != "png" && $file_type != "jpeg")
    {
        $upload_valid = false;
        $errors[] = "INVALID_FILE_TYPE";
    }

    //Verify the file is valid and upload it
    if ($upload_valid)
    {
        //the file is valid
        $temp = explode(".", $_FILES["file"]["name"]);
        $newfilename = round(microtime(true)) . '.' . end($temp);
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir . $newfilename))
        {
            $msg = array('FILE_UPLOAD_SUCCESS' => "File uploaded successfully");
            echo json_encode($msg);
        }
        else
        {
            $msg = array("FILE_UPLOAD_FAIL" => "Unable to upload file");
            echo json_encode($msg);
        }
    }
    else
    {
        //echo the error array in json
        echo json_encode($errors);
    }
}

0 个答案:

没有答案