gpt4 book ai didi

swift - 将图像从 Swift 应用上传到 PHP 端服务器

转载 作者:行者123 更新时间:2023-11-30 12:26:47 26 4
gpt4 key购买 nike

我目前正在用 Swift 制作一个应用程序,但对 PHP 的经验很少。我使用在线教程创建了一个侧服务器,以下是其代码:

index.php:

<html>
<head>
<title>PHP Upload Test</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" />
<input type ="hidden" name="partyGroupId" id="partyGroupId" value="10050"/>
<input type="submit" name="submit" value="Click to Upload"/>
</form>
</body>
</html>

上传.php:

<?php //ignore this comment >
$uploadFolder = "./upload";
if (!file_exists($uploadFolder)) {
mkdir($uploadFolder);
}

$uploadFile = $uploadFolder . "/" . basename($_FILES["fileToUpload"]
["name"]);

if(!(getimagesize($_FILES["fileToUpload"]["tmp_name"]) !== false)) {
echo "Sorry, your image is invalid";
exit;
}

$imageFileType = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));
if($imageFileType != "jpg"
&& $imageFileType != "png"
&& $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
exit;
}

if ($_FILES["fileToUpload"]["size"] > 2000000) {
echo "Sorry, your file is too large.";
exit;
}

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$uploadFile)) {
echo "Successfully";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>

Swift 3.0 代码:

func UploadRequest()
{
let url = URL(string: "http://localhost:8080")

let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"

let boundary = generateBoundaryString()


request.setValue("multipart/form-data; boundary=\(boundary)",
forHTTPHeaderField: "Content-Type")

if (notesImage.image == nil)
{
return
}

let image_data = UIImagePNGRepresentation(notesImage.image!)


if(image_data == nil)
{
return
}


let body = NSMutableData()

let fname = "test.png"
let mimetype = "image/png"


body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)



body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)


body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)


request.httpBody = body as Data



let session = URLSession.shared


let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in

guard ((data) != nil), let _:URLResponse = response, error == nil else {
print("error")
return
}

if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
{
print(dataString)
}

})

task.resume()


}


func generateBoundaryString() -> String
{
return "Boundary-\(UUID().uuidString)"
}

}

当我运行 iOS 应用程序时,它总是给出消息:抱歉,您的图像无效。我尝试过不同的图像,但它不起作用。我该怎么办?

最佳答案

您的 php 期望图像为“fileToUpload”,但在 Swift 中您使用名称“file”,因此 php 代码将无法获得预期的图像。

关于swift - 将图像从 Swift 应用上传到 PHP 端服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com