I am trying to upload a simple image from iOS Photo Library or Camera to my server using PHP 5.4 Moving the uploaded file fail, assuming $_FILES contains no data
func uploadImage() {
let image = self.imageView.image!
//let image = UIImage.init(named: "myImage")
let imgData = UIImageJPEGRepresentation(image, 1)!
print(imgData)
let parameters = ["name": "Frank Bergmann"]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "userfile",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:"https://myserver.de/apple_imageupload.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value!)
}
case .failure(let encodingError):
print(encodingError)
}
}
}
SWIFT 3 with Alamofire
PHP-Code on server
<?php
//$files = print_r($_FILES, 1);
//echo json_encode($files);
$uploaddir = "../bookcase/";
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$data=Array("Reply"=>"Image $file saved at server (bookcase)");
} else {
$data=Array("Reply"=>"Image not saved - Name:".$_FILES['userfile']['name']);
}
echo json_encode($data);
Output in xcode
Select Button pressed
Upload Button pressed
208743 bytes
{"Reply":"Image not saved - Name:"}
Assuming $_FILES is not present after upload - can help me anyone