Capture image and upload to server using PhoneGap

Client Side Script

PhoneGap Capture Image API: Capture Image PhoneGap API

//Camera Section
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
function onDeviceReady() {}

// Called when capture operation is finished
function captureSuccess(mediaFiles) {    

// Called if something bad happens.
function captureError(error) {
    var msg = 'An error occurred during capture: ' + error.code;
    navigator.notification.alert(msg, null, 'Uh oh!');

// A button will call this function
function captureImage() {
    // Launch device camera application,
    // allowing user to capture only one image by {limit: 1}
    navigator.device.capture.captureImage(captureSuccess, captureError, { limit: 1 });

// Upload files to server
function uploadFile(mediaFile) {
    path = mediaFile.fullPath;
    name =;
    var options = new FileUploadOptions();

    var params = new Object();
    params.fullpath = path; = name;

    options.params = params;
    options.chunkedMode = false;
    var ft = new FileTransfer();
    ft.upload( path, "",
        function(result) {
			//upload successful            
        function(error) {
            //upload unsuccessful, error occured while upload. 

Server Side Code

    $new_image_name = $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], WWW_ROOT . "/products/" . $new_image_name);

20 responses to “Capture image and upload to server using PhoneGap

    • WWW_ROOT . “/products/” , i have used because i used CakePHP at Back-end of PhoneGap app to upload the image. you can use it normally in stead of what i have used. You can see the following sample example

      $uploads_dir = '/uploads';
      foreach ($_FILES["pictures"]["error"] as $key => $error) {
          if ($error == UPLOAD_ERR_OK) {
              $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
              $name = $_FILES["pictures"]["name"][$key];
              move_uploaded_file($tmp_name, "$uploads_dir/$name");



  1. Hello,

    How is the Javascript communicating with the PHP code? And where do I place this code?

    Do I create a PHP file and place it on my server?

    Sorry, my PHP is limited only to using forms.

    Thanks in advance!


    • Hello @Paul, Thanks for comment

      Javascript communicate with php using Ajax request.

      yes, you have to create a php file to get the ajax request on your server.

      if your php have the limitation then solve that problem first to make this happen.


  2. Hello!

    my PHP file don’t received any value for $_FILES;

    and function ft.upload runs correctly. this function sents image bytes and returns success alert.

    I have send a file to my PHP file from a html form and this way the PHP file receives the file.

    it could be a problem of my server?


    • Hi Pedro,
      Thanks for your comment and sorry to late reply. I am not sure why this is happening. My code is 100% working and lots of Guy already used it. You need to trace the problem. You can take help/suggestion from web search. May be its a server issue.



  3. Hi,
    I am new to this, I’m not really suere where I’m supposed to put the upload.php file. Not in my phonegap folder, but where in my laptop?
    ( I have easyphp, express iis and xampp)


    • Hi, thanks for comment. First of all you seems to be very new to development, that fine & congrats for your approach.
      secondly the upload.php file should be placed in any server folder as you using xampp for example and ajax request from phone application will hit that upload.php to upload the the file/image that you sent from phone. But remember where you placed the upload.php file the path must be included in you ajax url at phonegap.

      Hope you get the answer.



  4. Which function should i call in your js so that when i click on upload button it capture the image than.
    Also what should i write in move_upload function in php so that i can move image to D: drive of my system


  5. Hi ziyedbd,

    I am new to phone gap,I am not getting exactly the php code required on server can you please forward the php code which I have to put on server to make it working…….


    • Hi Amit,

      I have posted the sample server code at the bottom of my post, see that.
      Just connect your DB, get the ajax request at server using php script and return the reply to ajax request to complete the process.



  6. It is working on android but it gives me issue on iphone as my image can not be uploaded ,we have added all camera permissions in plist.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s