Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:`试图从AngularJS获取PHP上载中非对象的属性' 在angularjs+php+mysql中上传文件_Php_Mysql_Angularjs_File Upload - Fatal编程技术网

错误:`试图从AngularJS获取PHP上载中非对象的属性' 在angularjs+php+mysql中上传文件

错误:`试图从AngularJS获取PHP上载中非对象的属性' 在angularjs+php+mysql中上传文件,php,mysql,angularjs,file-upload,Php,Mysql,Angularjs,File Upload,嘿,伙计们, 我做了简单的上传,一切都很好,但我有一个问题,上传一个文件,并添加到表中的其他信息 app.directive("fileInput", function ($parse) { return { link: function ($scope, element, attrs) { element.on("change", function (event) { var

嘿,伙计们, 我做了简单的上传,一切都很好,但我有一个问题,上传一个文件,并添加到表中的其他信息

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 = htmlspecialchars($_POST["server_id"]);
我不知道将
服务器id的数据放在哪里,或者它为什么向我发送错误

试图在第5行的upload.php中获取非对象的属性


如果我只在tbl_文档中添加文件,一切都会很好

$insertQuery = " INSERT INTO tbl_document(file_name) VALUES ('".$_FILES['file']['name']."'); "; 
最后,我做到了

对于每个可能需要帮助的人:

angular.forEach($scope.files, function (file) {
    form_data.append('file', file);
    form_data.append('server_id', $scope.info_mainServerID);
});
和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 = htmlspecialchars($_POST["server_id"]);
我不知道它有多安全,但它能工作。我愿意改进。
谢谢

首先,我会做一些转储,echo$path和/或$insertQuery,以确保一切都如预期的那样。还要检查您是否获得了“upload.php”中的$_文件['file']。