使用AngularJS,PHP和MySQL发布数据

时间:2015-04-21 08:05:31

标签: php angularjs slim

我想在使用PHP Rest服务时将图像作为blob存储到我的数据库(MySQL)中,但我不知道该怎么做。这是我的PHP代码(我使用的是PHP的Slim框架)

function addProblem() {
global $app;
$postdata = file_get_contents("php://input");
$req = json_decode($postdata); // Getting parameter with names
$paramName = $req->station; // Getting parameter with names
$paramAdres = $req->address; // Getting parameter with names
$paramCity = $req->city;// Getting parameter with names
$parampostal = $req->postalcode;
$parampic = $req->pictureOfDamage;
$paramdescrip= $req->description;
$sql = "INSERT INTO problems (Station,Address,Postalcode,City,PictureOfDamage,Description) VALUES (:station,:address,:postalcode,:city,:pictureOfDamage,:description)";
try {
    $dbCon = getConnection();
    $stmt = $dbCon->prepare($sql);  
    $stmt->bindParam(':station', $paramName);
    $stmt->bindParam(':address', $paramAdres);
    $stmt->bindParam(':city', $paramCity);
    $stmt->bindParam(':postalcode', $parampostal);
    $stmt->bindParam(':pictureOfDamage', $parampic);
    $stmt->bindParam(':description', $paramdescrip);
    $stmt->execute();
    $dbCon = null;
    echo json_encode("toegevoegd ");

} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}';

}

}

这是我的角度代码(我现在正在使用fileuploader。)

        .controller('MeldingController', function ($scope,  $upload, $rootScope, $state, $http) {
        $scope.station = $rootScope.station;
        $scope.PictureOfDamage;
        $scope.upload = function (files) {
            if (files && files.length) {
                for (var i = 0; i < files.length; i++) {
                    var pictureOfDamage = files[i];
                    return pictureOfDamage;
                }
            }
        }
        $scope.submit = function () {
            console.log($scope.PictureOfDamage);
            var data = {
                station: $scope.station.name,
                address: $scope.station.streetName,
                postalcode: $scope.station.postalCode,
                city: $scope.station.city,
                pictureOfDamage: $scope.upload($scope.files) /* picture*/,
                description: document.getElementById("Description").value
            }
            console.log('NOJSN ', data);
            data = JSON.stringify(data);
            console.log('JSON', data)
            $http({
                method: "POST",
                url: 'http://localhost/Dats24/problem/add/',
                data: data})
                    .success(function (data, status, headers, config) {
                        $state.go('GoogleMaps');
                    }).error(function (data, status, headers, config) {
                console.log(data);
            });

        };
    })

1 个答案:

答案 0 :(得分:0)

对于角度应用程序,您可以使用$ upload服务的上传方法,如下所示:

file_upload: function(file) { return $upload.upload({ url: 'http://your-upload-url/', file: file }); }

如下所述:https://github.com/danialfarid/ng-file-upload

然后在PHP服务中,您可以使用

获取文件

move_uploaded_file($_FILES['file']['tmp_name'], $file_path);

它会将文件存储在您选择的路径中,然后您可以使用PHP对文件数据执行任何操作。