我试图将一张图片上传到服务器,但是我的代码无法正常工作。这是我的快速代码:
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet weak var theOneImage: UIImageView!
let url = "http://xxx.xxx.xxx.xxx/Twitter/UploadImage.php"
let avaImage = UIImage(named: "cupid")
override func viewDidLoad() {
super.viewDidLoad()
theOneImage.image = avaImage
uploadImage(image: avaImage)
}
func uploadImage(image : UIImage?){
let imageData = avaImage?.pngData()
Alamofire.upload(multipartFormData: { (MultipartFormData) in
MultipartFormData.append(imageData!, withName: "image")
}, to: url) { (encodingResult) in
switch encodingResult{
case .success(let upload, _, _):
upload.responseJSON{ response in
print(response.request!) // original URL request
print(response.response!) // URL response
print(response.data!) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}
这是我的php代码:
if (empty($_FILES["image"])){
$response = array("error" => "no data");
}else{
$response["error"] = "NULL";
$filename = uniqid() . ".jpg";
if(move_uploaded_file($_FILES['image']['test'],'/ava')){
$response["status"] = "success";
$response["filepath"] = "http:" . $filename;
$response["filename"] = "" . $_FILES["file"]["name"];
}else{
$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"];
}
}
echo json_encode($response);
我得到的答复告诉我,在UploadImage.php中,$ _ FILE [“ image”]为空
这是我的服务器目录: server screen shot
请帮助。谢谢大家!