我正在使用$ cordovaFileTransfer将图片,音频,视频和PDF文件从我的Ionic Application上传到PHP服务器。我成功上传图片但未能将视频,音频和pdf文件上传到服务器。
接下来,我也无法将其他参数传递给我的PHP服务器。
以下是我的代码。
前端代码
function onSuccess(imageData) {
var url = "http://131.4.44.69/php2/attachment.php";
var options = {
fileKey: "file",
chunkedMode: false,
mimeType: "multipart/form-data",
params : {
"usernames": usernames,
"usernameID" : usernameID,
"subscribeChannel" : subscribeChannel,
"channelID" : channelID,
"attachment" : attachment
}
};
$cordovaFileTransfer.upload(url, imageData, options).then(function(result) {
alert('Image Successfully uploaded');
},function(err){
alert(err);
});
后端代码
<?php
header('Access-Control-Allow-Origin: *');
$imageName = date("d.m.Y H:i:s");
$imageName = preg_replace('/\s+/', '', $imageName);
$imageName = str_replace(',', '', $imageName);
$imageName = str_replace(':', '', $imageName);
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$usernames = $_FILES['file']['usernames'];
$usernameID = $_FILES['file']['usernameID'];
$subscribeChannel = $_FILES['file']['subscribeChannel'];
$channelID = $_FILES['file']['channelID'];
$attachment = $_FILES['file']['attachment'];
$target_path = 'image_attachment' . '/' . $imageName . '.'. $file_ext ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "Upload and move success";
} else{
echo "There was an error uploading the file, please try again!";
}
?>