Rei
Rei

Reputation: 143

How to get image name after upload to server in cordova

Hey guys i am using jquery, ajax and html. This is my upload photo

    function uploadPhoto(imageURI) {
         if (imageURI.substring(0,21)=="content://com.android") {
          photo_split=imageURI.split("%3A");
          imageURI="content://media/external/images/media/"+photo_split[1];
        }
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        //alert(options.fileName);
        options.mimeType="image/jpeg";

        var params = new Object();
        params.value1 = "test";
        params.value2 = "param";

        options.params = params;
        options.chunkedMode = false;

        var ft = new FileTransfer();
        ft.upload(imageURI, host+"/skripsitemplate/php/upload.php", win, fail, options);
    }

    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

This code is work fine. But i don't know how to get image name which has been uploaded to server. Image name will be uploaded to database. And this is my upload.php

<?php
print_r($_FILES);
$new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name);
echo $new_image_name;
?>

I am really newbie about cordova and phonegap things. Thank you guys and have a nice day

Upvotes: 0

Views: 74

Answers (1)

Shadi Shaaban
Shadi Shaaban

Reputation: 1720

According to your PHP code, the filename of the image that was uploaded is replied back as a response in echo $new_image_name; and you could access it in the win function as the object response:

 function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response); 
    console.log("Sent = " + r.bytesSent);
    alert("filename is" + r.response); // <-- this is your filename
}

However, you need to remove the print_r function on the $_FILES object as it will provide additional output that is not needed.

<?php
// print_r($_FILES); // <---- remove this 
$new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name);
echo $new_image_name;
?>

Upvotes: 2

Related Questions