How to upload image file to remote server in ionic framework

I want to upload an image to my remote server in ionic. it is in the following code;

1-First of all create a file under your remote server which name is uploadphp 

2-Secondly in your app.js file create a new controller

.controller(“ExampleController”, function($scope, $cordovaFileTransfer) {

$scope.upload = function() {
var options = {

fileType: “jpg”
};
$cordovaFileTransfer.upload(“http://example.com/fileupload.php”,cordova.file.externalRootDirectory + “Pictures/1.jpg”, options).then(function(result) {
console.log(“SUCCESS: ” + JSON.stringify(result.response));
}, function(err) {
console.log(“ERROR: ” + JSON.stringify(err));
}, function (progress) {
// constant progress updates
});
}

})

3- and thirdly  for catch the image ,create a php file it’s name is fileupload.php
<?php
// Allowed extentions.
$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);

// Get filename.
$temp = explode(“.”, $_FILES[“file”][“name”]);

// Get extension.
$extension = end($temp);

// An image check is being done in the editor but it is best to
// check that again on the server side.
// Do not use $_FILES[“file”][“type”] as it can be easily forged.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES[“file”][“tmp_name”]);

if ((($mime == “image/gif”)
|| ($mime == “image/jpeg”)
|| ($mime == “image/pjpeg”)
|| ($mime == “image/x-png”)
|| ($mime == “image/png”))
&& in_array(strtolower($extension), $allowedExts)) {
// Generate new random name.
$name = sha1(microtime()) . “.” . $extension;

// Save file in the uploads folder.
move_uploaded_file($_FILES[“file”][“tmp_name”], getcwd() . “/uploadphp/” . $name);

// Generate response.
$response = new StdClass;
$response->link = “/uploadphp/” . $name;
echo stripslashes(json_encode($response));
}
?>

Figen Önün Kâşif on 29 Aralık 2015 yazdı
Yorum Ekle
  • 1 Yanıt(lar)

    thanks a lot for this information. :)

    Semiha Makinist Uzman on 29 Aralık 2015 yanıtladı
    Yorum Ekle
  • Yanıtınız

    Cevabınızı göndererek Gizlilik Politikası ve Hizmet Şartlarını kabul ediyorsunuz.