File-Transfer Cordova how to use php

Asked

Viewed 480 times

2

Hello I have the following code

 onDeviceReady: function () {
    navigator.camera.getPicture(
                app.uploadPhoto,
                function (message) { alert('get picture failed'); },
                {
                    quality: 50,
                    destinationType: navigator.camera.DestinationType.FILE_URI,
                    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
                }
            );
},
uploadPhoto: function(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://192.168.0.101/upload/index.php"), app.win, app.fail, options);
},

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

fail: function(error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
},

It is working, however when I upload to my php I do not know how to save, my php is like this:

    <?php
move_uploaded_file($_FILES["file"]["tmp_name"], 'C:\xampp\htdocs\upload\images');
    ?>

I’d like to save inside C:\xampp\htdocs\upload\images, how my php should look?

1 answer

1

I managed to make it work as follows, the js was like this.

ft.upload(fileURI, encodeURI("http://192.168.0.102/upload/index.php"), app.win, app.fail, options);

index.php takes and sends it to the 'images' folder inside 'upload'.

 <?php
    $new_image_name = strtolower($_FILES['file']['name']);
    move_uploaded_file($_FILES["file"]["tmp_name"], "images/".$new_image_name);
  • If your answer is correct, then mark it as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.