Search code examples
phpjavascriptcordovafile-transfer

Phonegap image upload fail to upload


I am follow this tutorial for uploading images to a server using Phonegap. The data gets sent to the server but fails to upload. This is the response I get when I try to upload a picture to my server.

Response

here is my php code

<?php
  header("access-control-allow-origin: *");
  print_r($_FILES);
  $new_image_name = "image.jpg";
  move_uploaded_file($_FILES["file"]["tmp_name"], "/products/".$new_image_name);
?>

and here is my function to run the whole thing on my app

    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://clubbedin.clanteam.com/upload.php", win, fail, options);
    }

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

    function fail(error) {
        alert("An error has occurred: Code = " = error.code);
    }

Here is the structure for the server directory


Solution

  • the path is wrong, in that case. /products/ might be at the root when you log in, but the web user might have a different root. It's worth checking, at least - if you upload a PHP file containing just echo(getcwd()); they should show you what PHP thinks the path is. - @andrewsi