Загрузка изображений не работает с быстрым alamofire и переполнением стека

Попробовав разные подходы, я не смог найти причину проблемы …

Я пытаюсь загрузить изображение (выбранное из библиотеки фотографий), чтобы сохранить его на веб-сервере linux, используя скрипт php.

Вот код 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";
}

Может кто-нибудь дать мне подсказку, чтобы решить мою проблему?

заранее спасибо

0

Решение

Я думаю, что у вас есть ошибка в move_uploaded_file Вы даете ключ, которого нет в $ _FILES global

Должно быть как
move_uploaded_file($_FILES['image']['tmp_name'], '/default/' . $filename)

И убедитесь, что папка / каталог, куда вы перемещаете файл, доступна для записи.

1

Другие решения

Других решений пока нет …