Calculating Machine Calculating Machine - 26 days ago 4
Javascript Question

cordova: image upload not working

I am using cordova file upload plugin to upload image. I am trying to upload image from gallery to server but due to some reason file is not uploading to server.

Here is the js code.

function uploadImage() {
document.getElementById('picture_msg').innerHTML = "";
// Get URI of picture to upload
navigator.camera.getPicture(
function(uri) {
try {
// Pick image from div
var img = document.getElementById('pimage');
img.style.visibility = "visible";
img.style.display = "block";
var imageURI = uri;
if (!imageURI || (img.style.display == "none")) {
document.getElementById('picture_msg').innerHTML = "Tap on picture to select image from gallery.";
return;
}
// Verify server has been entered
server = document.getElementById('server').value;
server = 'http://domain_name/mobile_app/img_upload.php';
console.log("Server "+server);
if (server) {
// Specify transfer options
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;

// Transfer picture to server
var ft = new FileTransfer();
ft.upload(imageURI, server, function(r) {
document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
img.src = uri;
img.width = 100;
img.height = 100;
},
function(error) {
document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
}, options);
}
else {
document.getElementById('picture_msg').innerHTML = "Server Not Found";
}
}
catch(exce) {
alert(exce);
}
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('picture_msg').innerHTML = "No Image Found";
},
{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
}


And here is the img_upload.php code.

print_r($_FILES);
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "mobile_app/".$new_image_name);


I tried this as well.

$dirname = "user_img/";
// If uploading file
if ($_FILES) {
print_r($_FILES);
// mkdir ($dirname, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
}


I am getting upload successful message but when i check the data on server it is showing empty. No files are there.

Please advise what am i doing wrong here.

Thanks

Edit

It seems like problem with php file. path in webserver is
/public_html/app/user_imgs


my php file img_upload.php is under app folder.

I am not sure what to do now. I checked multiple options from google but this is not working.

Answer

Finally after 7 days i fixed the issue. Actually i was very stupid. You will laugh on me.

The url i had given was http://sample.com/upload_img.php i just need to add www in the url. So, working URL is http://www/sample.com/upload_img.php. It fixed the issue.

I hope someone else will get the help from it.