Reputation: 15
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
Upvotes: 0
Views: 1722
Reputation: 94
Change the "1" in your declare for imgData to 0.1
let imgData = UIImageJPEGRepresentation(image, 0.1)!
That changes the quality of the image uploaded. Where 1 being the highest quality and 0 being the lowest (most compressed). I had the same issue when I used "1" but worked for me when I set to a lower quality. The image quality was noticeably bad for me so this solution may work for you too. You can also try another quality/compression amount, such as 0.2 or 0.5 or higher. That may also work for you, you just have to test it.
I speculate the reason for this is that the image was too big (when not compressed) for the server to handle it.
Upvotes: 1