I'm finding that if you upload an image using phonegap with php on the server, it works every other time. Consistently. The first upolad succeeds, the second fails, the third succeeds, the fourth fails and so on.
I'm using an example located here: How to retrieve POST data from PhoneGaps File Transfer API
And I'm using an android phone to test.
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {
console.log("device ready");
// Do cool things here...
}
function getImage() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto, function(message) {
alert('get picture failed');
}, {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
function captureImage() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto, function(message) {
alert('get picture failed');
}, {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI
});
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
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, "http://someserver.com/somedir/up.php", win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode.toString() + "\n");
console.log("Response = " + r.response.toString() + "\n");
console.log("Sent = " + r.bytesSent.toString() + "\n");
alert("Code Slayer!!!");
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
}
And php
<?php
print_r($_FILES);
$new_image_name = "YEAH.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/".$new_image_name);
?>