Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php can';在swift 3中使用Alamofire无法上传图像_Php_Json_Swift3_Alamofire - Fatal编程技术网

Php can';在swift 3中使用Alamofire无法上传图像

Php can';在swift 3中使用Alamofire无法上传图像,php,json,swift3,alamofire,Php,Json,Swift3,Alamofire,我在试图让Alamofire上传图像时被困了三天。其想法是,Alamofire将使用一些php代码将其发送到服务器。在不同的地方进行了大量的尝试和研究之后,一些代码应该可以工作,但是Alamofire的服务器端文档非常糟糕 swift 3的最新更新对答案没有多大帮助 这是我的Swift 3代码: let imageData = UIImageJPEGRepresentation(imageFile!, 1)! Alamofire.upload( multipartFormDat

我在试图让Alamofire上传图像时被困了三天。其想法是,Alamofire将使用一些php代码将其发送到服务器。在不同的地方进行了大量的尝试和研究之后,一些代码应该可以工作,但是Alamofire的服务器端文档非常糟糕

swift 3的最新更新对答案没有多大帮助

这是我的Swift 3代码:

let imageData = UIImageJPEGRepresentation(imageFile!, 1)!

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "file/jpeg")
        },
        to: "https://someadress.com/post/upload.php",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )
这应该上传到服务器上的图像,但我不知道如何正确地将图像保存在服务器上。服务器实际上不需要该文件的任何信息,因为它将为该文件生成一个新名称。然后,它应该将该名称发送回应用程序

我知道如何在Swift 3和php中处理JSON,因为我以前做过。我还确信至少有一些东西被上传到了服务器上,因为我已经得到了一些基本信息

下面的PHP代码几乎肯定不是很好,但它主要是一个测试

<?php
// get the file data
$fileData = file_get_contents('php://input');

// sanitize filename
$fileName = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).])", '', $fileData);
// save to disk

$fileLocation = "../images/" . $fileName;
file_put_contents($fileLocation, $fileData);

if (empty($fileData)) {
    $response = array("error" => "no data");
}
else {
    $response = array("error" => "ok " . $fileName);
}

echo json_encode($response);
?>

感谢您提前提供的帮助:)


p、 我是斯威夫特的新手,所以请温柔一点;)

好的,苏欧。我想出来了。事实证明,Alamofire使用了php的
$\u FILES
函数。没有提到这一点,所以让我来尝试澄清一下。下面是完整的PHP代码和注释

<?php

// If the name of the image is not in this array, the app didn't post anything.
if (empty($_FILES["image"])) {
    // So we send a message back saying there is no data...
    $response = array("error" => "nodata");
}
// If there is data
else {
    $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']['tmp_name'], "../images/" . $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'] = "[APILINK]/images/" . $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);
?>

我希望这能帮助很多有同样问题的人!如果有什么遗漏或你想知道的,请告诉我

好的,苏欧。我想出来了。事实证明,Alamofire使用了php的
$\u FILES
函数。没有提到这一点,所以让我来尝试澄清一下。下面是完整的PHP代码和注释

<?php

// If the name of the image is not in this array, the app didn't post anything.
if (empty($_FILES["image"])) {
    // So we send a message back saying there is no data...
    $response = array("error" => "nodata");
}
// If there is data
else {
    $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']['tmp_name'], "../images/" . $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'] = "[APILINK]/images/" . $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);
?>
我希望这能帮助很多有同样问题的人!如果有什么遗漏或你想知道的,请告诉我

swift3

 func uploadImage(_ imageFileUrl:URL, encodeCompletion: ((Alamofire.SessionManager.MultipartFormDataEncodingResult) -> Void)?){
     let fileName = "imageName.jpg"
     let headers = ["contentType":"image/jpeg"]
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageFileUrl, withName: fileName)
        }, to: "uploadPath", method: .post, headers: headers, encodingCompletion: encodeCompletion)
    }
斯威夫特3

 func uploadImage(_ imageFileUrl:URL, encodeCompletion: ((Alamofire.SessionManager.MultipartFormDataEncodingResult) -> Void)?){
     let fileName = "imageName.jpg"
     let headers = ["contentType":"image/jpeg"]
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageFileUrl, withName: fileName)
        }, to: "uploadPath", method: .post, headers: headers, encodingCompletion: encodeCompletion)
    }

我修好了!我会在几天后把它添加到文档中!您好,您能给出您的解决方案来帮助社区吗?thanks@Sam完成!希望它能帮助别人!我修好了!我会在几天后把它添加到文档中!您好,您能给出您的解决方案来帮助社区吗?thanks@Sam完成!希望它能帮助别人!很好的评论和解释。很好的评论和解释。