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

时间:2018-01-07 13:50:34

标签: php swift alamofire alamofireimage

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

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

这里是Swift 4代码:

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";
}

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

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为你在move_uploaded_file函数中有错误。你给的是$ _FILES global

中没有的密钥

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

确保移动文件的文件夹/目录是可写的。