Php 使用swift中的多部分/表单数据上载带参数的图像
我正试图在服务器的特定路径上传图像。该路径将由从Php 使用swift中的多部分/表单数据上载带参数的图像,php,ios,swift,multipartform-data,Php,Ios,Swift,Multipartform Data,我正试图在服务器的特定路径上传图像。该路径将由从multipart/form data请求发送的参数确定 问题:图像未上载到指定路径,而是上载到我的服务器的根目录 我正在调用一个具有php函数的API,以将该图像保存到指定路径。我认为PHP函数没有正确地从参数中获取路径 我调用的API看起来像 upload_image.php的内容 <?php header('Access-Control-Allow-Origin: *'); //Unable to get path para
multipart/form data
请求发送的参数确定
问题:图像未上载到指定路径,而是上载到我的服务器的根目录
我正在调用一个具有php函数的API,以将该图像保存到指定路径。我认为PHP函数没有正确地从参数中获取路径
我调用的API看起来像
upload_image.php的内容
<?php
header('Access-Control-Allow-Origin: *');
//Unable to get path parameter and store in @target_dir, instead this function stores image in root
$target_dir = $_GET["path"];
$name = basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . $name;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$actual_name = strtolower(
pathinfo(
$_FILES["fileToUpload"]["name"],PATHINFO_FILENAME));
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo $name;
} else {
echo "Sorry, there was an error uploading your file.";
}
?>
注意:在php函数中,如果我将目录设置为常量字符串,它会保存在该路径中。就像我做了下面的事情
$target_dir = "Brainplow"
这很好,但我需要根据从multipart/formdata
请求发送的参数来确定这个目录
我的图像目录如下
根目录
子目录
孙子目录
因此,我希望能够将路径(例如:“Brainplow/011113132018112642/”)作为参数传递给请求
PHP函数应该从参数中获取路径并将图像放在那里
根据我的研究和努力,问题在于PHP函数。所以也许不用
$target_dir = $_GET["path"];
我可能不得不使用
$target_dir = $_POST["path"];
但我对PHP了解不多。但也许还有另一个问题。但我的swift代码运行良好。只是我的形象不在我提供的道路上
任何帮助都将不胜感激修改PHP函数 换了这条线
$target_dir = $_GET["path"];
到
它现在正在工作上传带有参数的多部分图像的完整解决方案
#import Alamofire
func multipartImage(data:Data?, url:String,jsonInput:[String: String], completion: @escaping (_ result: DataResponse<Any>) -> Void) {
Alamofire.upload(multipartFormData:
{ (multipartFormData) in
if data != nil {
multipartFormData.append(data!, withName:"user_image", fileName:"image.jpg", mimeType:"image/jpeg")
}else{
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "user_image")
}
for (key, value) in jsonInput
{
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:url, method: .post, headers: headers)
{ (result) in
switch result {
case .success(let upload, _ , _ ):
upload.uploadProgress(closure:
{ (progress) in
//Print progress
})
upload.responseJSON { response in
completion(response)
}
case .failure(let encodingError):
print(encodingError.localizedDescription)
break
}
}
}
#导入阿拉莫菲尔
func多部分图像(数据:data?,url:String,jsonInput:[String:String],完成:@escaping(uu-result:DataResponse)->Void){
Alamofire.upload(multipartFormData:
{(multipartFormData)中的
如果数据!=nil{
multipartFormData.append(data!,名称为:“user_image”,文件名为:“image.jpg”,mimeType:“image/jpeg”)
}否则{
multipartFormData.append(“.data(使用:String.Encoding.utf8)!,名称为:“user\u image”)
}
用于jsonInput中的(键、值)
{
multipartFormData.append(value.data(使用:String.Encoding.utf8)!,with name:key)
}
},收件人:url,方法:。发布,标题:标题)
(结果)
切换结果{
成功案例(让我们上传,,u):
upload.uploadProgress(关闭:
(进展)
//打印进度
})
upload.responseJSON{中的响应
完成(回应)
}
案例失败(let encodingError):
打印(编码错误。本地化描述)
打破
}
}
}
有什么理由否决这个问题吗?“我可能必须使用$target\u dir=$\u POST[“path”];”-你试过了吗?但没有试过。现在就去试试在发帖之前,你应该尽你所能自己解决问题。请在所有的挣扎之后阅读。这就是问题所在。我必须更改$target\u dir=$\u GET[“path”]
到$target\u dir=$\u POST[“path”]
您也可以使用$\u request[“path”]更多信息::此答案不相关。因为问题是关于如何将文件保存到请求参数设置的路径。正如我在问题中提到的,若我使用硬编码路径,我的swift代码运行良好。
$target_dir = $_POST["path"];
#import Alamofire
func multipartImage(data:Data?, url:String,jsonInput:[String: String], completion: @escaping (_ result: DataResponse<Any>) -> Void) {
Alamofire.upload(multipartFormData:
{ (multipartFormData) in
if data != nil {
multipartFormData.append(data!, withName:"user_image", fileName:"image.jpg", mimeType:"image/jpeg")
}else{
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "user_image")
}
for (key, value) in jsonInput
{
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:url, method: .post, headers: headers)
{ (result) in
switch result {
case .success(let upload, _ , _ ):
upload.uploadProgress(closure:
{ (progress) in
//Print progress
})
upload.responseJSON { response in
completion(response)
}
case .failure(let encodingError):
print(encodingError.localizedDescription)
break
}
}
}