图片上传不适用于 swift alamofire 和 php

Image Upload doesn't work with swift alamofire and php

尝试了不同的方法后,我找不到问题的原因...

我正在尝试使用 php 脚本上传图像(从照片库中选择)以将其存储在 linux 网络服务器上。

这里是Swift4码:

func uploadImage(imageFile: UIImage?){

    let imageData = UIImageJPEGRepresentation(imageFile!, 0.5)!

    Alamofire.upload(
        multipartFormData: { multipartFormData in

            multipartFormData.append(imageData, withName: "image", fileName: "test", mimeType: "image/jpg")

        },
        to: "http://XXX/images/upload.php", method: .post,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    if let result = response.result.value {
                        // Get the json response. From this, we can get all things we send back to the app.
                        //let JSON = result as! NSDictionary
                        //self.imageServerLocation = JSON.object(forKey: "filepath") as? String
                        debugPrint(response)
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )
}

服务器代码如下:

<?php
if (empty($_FILES["image"])) {
    // So we send a message back saying there is no data...
    $response = array("error" => "nodata");
}else { // If there is data
    $response['error'] = "NULL";
    // Setup a filename for the file. Uniqid can be changed to anything, but this makes sure
    // that every file doesn't overwrite anything existing.
    $filename = uniqid() . ".jpg";
    // If the server can move the temporary uploaded file to the server
    if (move_uploaded_file($_FILES['image']['test'], '/default/' . $filename)) {
        // Send a message back saying everything worked!
        // I also send back a link to the file, and the name.
        $response['status'] = "success";
        $response['filepath'] = "https:" . $filename;
        $response['filename'] = "".$_FILES["file"]["name"];
    } else{
        // If it can't do that, Send back a failure message, and everything there is / should be form the message
        // Here you can also see how to reach induvidual data from the image, such as the name.
        $response['status'] = "Failure";
        $response['error']  = "".$_FILES["image"]["error"];
        $response['name']   = "".$_FILES["image"]["name"];
        $response['path']   = "".$_FILES["image"]["tmp_name"];
        $response['type']   = "".$_FILES["image"]["type"];
        $response['size']   = "".$_FILES["image"]["size"];
    }
}
// Encode all the responses, and echo them.
// This way Alamofire gets everything it needs to know
echo json_encode($response);
?>

我总是得到同样的错误:

[Data]: 107 bytes
[Result]: SUCCESS: {
    error = 0;
    name = test;
    path = "/tmp/php7iIqSv";
    size = 43337;
    status = Failure;
    type = "image/jpg";
}

有人可以给我提示来解决我的问题吗?

提前致谢

我认为你在 move_uploaded_file 中有错误 function.You 提供的密钥在 $_FILES global

中不存在

应该是这样的 move_uploaded_file($_FILES['image']['tmp_name'], '/default/' . $filename)

并确保要移动文件的 folder/directory 是可写的。