2015-04-21 67 views
1

我想在使用PHP Rest服務時將圖像作爲blob存儲到我的數據庫(MySQL)中,但我不知道如何去做。這是我的PHP代碼(我使用PHP框架修身)使用AngularJS,PHP和MySQL發佈數據

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); 
      }); 

     }; 
    }) 

回答

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用文件數據做你想做的任何事情。