Phonegap upload issue with PHP server

505 views
Skip to first unread message

Saikat Saha

unread,
Jan 30, 2014, 6:00:35 AM1/30/14
to eclipse-plugi...@googlegroups.com
Hi, I am using the below phonegap code to upload my file to remote PHP server and I am able to upload the file even  I am getting response 200 ok but on the PHP server my file is not getting received even the array is empty, I don't know PHP coding while my friend can't able to figure out the issue in PHP code,could anyone please help me out with the required PHP code we had tried every possible steps.


PHP CODE

<?php

  print_r($_FILES);
  
   $results = print_r($_FILES, 1) ;
   
   file_put_contents('mahi.txt', $results);

   print "<br>";
if ($_FILES["file"]["error"] > 0) {
  echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
  
  file_put_contents('ranga.txt', "reddy");
  echo "Upload: " . $_FILES["file"]["name"] . "";
  echo "Type: " . $_FILES["file"]["type"] . "";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); //Save location
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>


PHONEGAP CODE


<!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>PhoneGap</title>
    <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="jquery-1.8.0.js"></script>
    <script type="text/javascript" charset="utf-8">
  
    var statusDom;
    
    var deviceReady = false;
 

    /**
     * Function called when page has finished loading.
     */
    function init() {
        document.addEventListener("deviceready", function() {deviceReady = true;}, false);
        window.setTimeout(function() {
            if (!deviceReady) {
                alert("Error: PhoneGap did not initialize.  Demo will not run correctly.");
            }
        },2000);
    }
  
    function selectPicture() {
        navigator.camera.getPicture(
            function(uri) {
                var img = document.getElementById('camera_image');
                img.style.visibility = "visible";
                img.style.display = "block";
                img.src = uri;
                document.getElementById('camera_status').innerHTML = "Success";
                statusDom = document.querySelector('#statuss');
            },
            function(e) {
                console.log("Error getting picture: " + e);
                document.getElementById('camera_status').innerHTML = "Error getting picture.";
            },
            { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,mediaType: navigator.camera.MediaType.ALLMEDIA});
    };
  
    /**
     * Upload current picture
     */
     function uploadPicture() {
       
         // Get URI of picture to upload
          var img = document.getElementById('camera_image');
          var imageURI = img.src;
          if (!imageURI || (img.style.display == "none")) {
              document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
              return;
          }
        
          // Verify server has been entered
          server = document.getElementById('serverUrl').value;
          if (server) {
              
              // Specify transfer options
              var options = new FileUploadOptions();
              options.fileKey="file";
              options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            
              options.mimeType="image/jpeg";
              options.mimeType="image/png";
              options.mimeType="image/gif";
              options.mimeType = "audio/mp3";
              options.mimeType = "audio/mpeg";
              options.mimeType = "audio/wav";
              options.mimeType = "video/mpeg";
              options.mimeType = "video/quicktime";
              options.mimeType = "video/mp4";
              options.mimeType = "video/ogg";
              options.chunkedMode = false;
            
              var params = {};
              params.value1 = "test";
              params.value2 = "param";
            
              options.params = params;



              // Transfer picture to server
              var ft = new FileTransfer();
              alert(options.fileName);
              ft.upload(imageURI, server, function(r) {
                  statusDom.innerHTML = "";
                  document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";               
              }, function(error) {
                  document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;                 
              }, options);
 
            
              ft.onprogress = function(progressEvent) {
                  if (progressEvent.lengthComputable) {
                      var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
                      statusDom.innerHTML = perc + "% loaded...";
                      } else {
                          if(statusDom.innerHTML == "") {
                              statusDom.innerHTML = "Loading";
                              } else {
                                  statusDom.innerHTML += ".";
                                  }
                          }
                  };
            
          }
      }




    </script>

  </head>
  <body onload="init();">
    <h3>PhoneGap Camera Upload Demo</h3>
  
    <div>
        <h3>Server URL for upload.php:</h3>
        <input id="serverUrl" type="text" value="https://testserver.com/FileUpload/upload_file.php" />
    </div>
    <br/>

       <!-- Camera -->
       <div>
              <h3>Camera:</h3>
              <b>Status:</b> <span id="camera_status"></span><br>
        <b>Image:</b> <img style="width:120px;visibility:hidden;display:none;" id="camera_image" src="" />
       </div>
       <div id="statuss"></div>
      
    <!-- Actions -->
    <div>
  
        <input type="button" onclick="selectPicture();" value="Select Picture from Library" /><br/>
        <input type="button" onclick="uploadPicture();" value="Upload Picture" />
    </div>
    <br/>
  
  </body>
</html>

Reply all
Reply to author
Forward
0 new messages