我目前正在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>
upload.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应用时,它始终会显示消息:抱歉您的图片无效。我尝试了不同的图像,但它无法正常工作。我该怎么办?
答案 0 :(得分:0)
你的php期望图像为&#34; fileToUpload&#34;,但是在Swift中你使用名称&#34; file&#34;,所以php代码将无法获得预期的图像。