用angularjs+php+mysql上传文件
嘿,伙计们,我做了简单的上传,一切都很好,但我有一个问题,上传一个文件,并添加到表与其他信息。
app.directive("fileInput", function ($parse) {
return {
link: function ($scope, element, attrs) {
element.on("change", function (event) {
var files = event.target.files;
$parse(attrs.fileInput).assign($scope, element[0].files);
$scope.$apply();
});
}
}
});
$scope.uploadFile = function () {
var form_data = new FormData();
var server_id = $scope.info_mainServerID;
console.log(server_id); // value that I need
angular.forEach($scope.files, function (file, data) {
form_data.append('file', file);
form_data.append('data', JSON.stringify(data));
});
$http.post('php/upload.php', form_data,
{
transformRequest: angular.identity,
headers: {'Content-Type': undefined, 'Process-Data': false},
// data: angular.toJson($scope.info_mainServerID)
server_id : $scope.info_mainServerID
})
.success(function (response) {
console.log(response);
alertify.notify(response, 'success', 2.5);
})
.error(function (response) {
console.log(response);
alertify.notify(response, 'error', 2.5);
});
};
以及upload.php
$data = json_decode(file_get_contents("php://input"));
$server_id = $data->server_id;
if(count($data) > 0){
if(!empty($_FILES)){
$path = '../documents/' . $_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)){
$insertQuery =
"
INSERT INTO tbl_document(server_id,file_name)
VALUES ($server_id,'".$_FILES['file']['name']."');
";
if(mysqli_query($connect, $insertQuery)){
echo 'File uploaded';
}
else{
echo 'File uploaded but not saved';
}
}
}
else{
echo("Error description: " . mysqli_error($connect));
}
}
我不知道把数据放在哪里 server_id
,或者它为什么给我发送错误
试图在5行的upload.php中获取非对象的属性
如果我只在tbl\u文档中添加文件,一切都会很好
$insertQuery = " INSERT INTO tbl_document(file_name) VALUES ('".$_FILES['file']['name']."'); ";
1条答案
按热度按时间7kjnsjlb1#
最后,我做到了!
对于每个可能需要帮助的人:
和upload.php
我不知道它有多安全,但它是有效的。我愿意改进。谢谢!