Problem with FileTransfer params


#1

Hi,

I have just switched hosting provider, and the code I use used to work fine on my old server. However, now the params that are sent with the options of the FileTransfer appear to be empty, as they are not inserted into the database correctly.

The connection to the database is working, as it inserts the parameters correctly if I set them manually in the PHP file itself.

Has anyone else experienced a problem like this? Is there a PHP configuration setting that I could change to fix it? At the minute I am flat out of ideas :pensive:


#2

I’m also having this issue. Anyone else?


#3

Any ideas? I’ll totally buy you a beer. I’m having a params issue with my server, too. It’s beginning to make me feel a bit crazy…I’ve tried a lot of variations.

In my case I need to pass params from a file transfer upload to my server-side php file when a photo is uploaded (should work with POST). I just can’t seem to receive anything from the params server-side except “1”.

All is swell receiving and doing stuff with the image file, and no problems manually setting attributes via PHP (such as the name and location of the new file), but no matter what I try the server only sees params as “1”.

Example file transfer portion:

$scope.uploadMe = function () {

var server = 'http://server.com/uploader.php';

    var trustAllHosts = true;
    
    var options = new FileUploadOptions();
	options.fileKey = 'file';
	options.fileName = "this";
	options.mimeType = "image/jpeg";
	options.httpMethod = 'POST';

	var params = {};
	params.value1 = "test1";
	params.value2 = "test2";

	options.params = params;

   $cordovaFileTransfer.upload(encodeURI(server), imageData, options, trustAllHosts);
   $ionicPopup.alert({
   title: 'Sweet.',
   content: 'That worked.'
 });

}

and example server side PHP:

print_r($_FILES);
$paramValue = $_POST[‘options’];
$new_image_name = print_r($paramValue[‘options’][‘params’][‘value1’]) . ‘@’ . date(‘g:i:sa \o\n l, M jS, Y’) . ‘.jpg’;
move_uploaded_file($_FILES[“file”][“tmp_name”], “images/”.$new_image_name);

…which returns a filename of “1 @ (some date and time).jpg” instead of including the param, which is this case should be “test1” – expected output is “test1 @ (some date and time).jpg”.

I’ve tried getting the params via PHP a lot of different ways with always the same result. So I have concluded that in fact the server is only receiving “1” for params from file transfer, no matter what the param may be.

Has anyone else been down this road? How can I send the actual param for the server to use in this way? Basically my users need the ability to manually set a param via an input box in order to tag their image file with some description before it uploads (e.g. “Wow look at all this foo @ (day/time).jpg”).

Many thanks to anyone willing to throw an idea at this!


#4

Hey man, I know the feeling. I’ll try my best to help you out.

I am not that brilliant at PHP, so I cannot say for sure if this would work or not. But the way my (now working) PHP code takes the parameters is like this;

$var1 = $_POST["value1"];

Have you tried using that? So using your code you could try;

$new_image_name =  print_r($_POST["value1"]) . '@' . date('g:i:sa \o\n l, M jS, Y') . '.jpg';

Let me know how you get on :slight_smile:

EDIT: I am actually fairly sure this should work for you. The way the option.params are sent are not through an actual “options” variable, but each are sent as their own post variable. So there is a POST variable sent for every option.param you add. You can then get these variables using the code I showed above, i.e.

 $var1 = $_POST["value1"];

#5

Hello and thank you for responding! I’m certainly not a PHP genius either – I’m basically just learning as I go…which can be rough on occasion :slight_smile:

I did try something very similar and still only got the response “1” from $_POST. I also just tried precisely what you suggested but the server still seems to only see “1” as the param value.

Would you mind terribly posting an excerpt of your file transfer upload function and the PHP it interacts with to accomplish successfully getting the params? It could be enormously helpful, perhaps it would be obvious what I’m overlooking!

Cheers


#6

That’s strange alright! Try setting the chunkedMode to true, I have it done in my code here;

(I have cut some of my code out quickly, hopefully nothing important)

 var imageURI = img.src;

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    options.headers = {Connection: "close"};
    options.chunkedMode = true;

var params = new Object();`
params.uname = $scope.username;
params.gid = $scope.selectedGroup;
params.rating = $scope.userRating;


options.params = params;

var ft = new FileTransfer();
ft.upload(imageURI, "http://path/to/php/upload.php", win, fail, options, true);

`

And here is what I am doing in my PHP; Again, I’ve cut avast chunk of this out, but this is the code relating to what you need I think.

$lati = $_POST["lat"];
$long = $_POST["lon"];
$usname = $_POST["uname"];

$insert = " INSERT INTO tablename VALUES (''$lati', '$long', '$usname', ) ";

Hopefully this helps. I think setting the chunkedMode to true may do the trick, I’ve read a few things about it here and there mentioning it can cause problems if it is not set to true. Could be wrong though.


#7

Thanks again for your help! Yes, it’s certainly a strange issue. Unfortunately I’m still only getting “1” on the server for any params!

As I said, I’m quite new at this and learning as I go, so I can’t help wondering if simply out of inexperience I’ve accidentally created some other issue in my setup that’s causing this oddity.

Looking at all elements from the beginning, I have this state:

.state('menu.share', {
  url: "/share",
        cache: 'false',
  views: {
    'menuContent': {
      templateUrl: "share.html",
      controller: 'imageController'
    }
  }
})

And this controller:

.controller('imageController', function($scope, $cordovaCamera, $ionicPopup, $cordovaFile, $cordovaFileTransfer) {

//Get Photo From Camera
$scope.getPhoto = function() {
console.log('Hello camera');
$cordovaCamera.getPicture({
  quality: 75,
  destinationType: Camera.DestinationType.FILE_URI,
  targetWidth: 320,
  targetHeight: 320,
  saveToPhotoAlbum: false
}).then(function(FILE_URI) {
  console.log(FILE_URI);
  $scope.lastPhoto = FILE_URI;
}, function(err) {
  console.err(err);
});
}

//Get Photo From Library
$scope.getLibrary = function() {
console.log('Hello library');
$cordovaCamera.getPicture({
  sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
  quality: 75,
  destinationType: Camera.DestinationType.FILE_URI,
  targetWidth: 320,
  targetHeight: 320,
  saveToPhotoAlbum: false
}).then(function(FILE_URI) {
  console.log(FILE_URI);
  $scope.lastPhoto = FILE_URI;
}, function(err) {
  console.err(err);
});
}

//Send To Server
$scope.uploadPhoto = function() {
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
$ionicPopup.alert({
   title: 'Sweet.',
   content: 'That worked.'
 });
}

var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
$ionicPopup.alert({
   title: 'Shit.',
   content: 'Not quite.'
 });
}

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = $scope.lastPhoto.substr($scope.lastPhoto.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
options.headers = {Connection: "close"};
options.chunkedMode = true;

var params = new Object();
params.name = "test";

options.params = params;

var ft = new FileTransfer();
ft.upload($scope.lastPhoto, encodeURI("http://www.server.com/uploader.php"), win, fail, options);
 }
 })

Sending to this PHP:

  <?php 
define('TIMEZONE', 'America/New_York');
date_default_timezone_set(TIMEZONE);
print_r($_FILES);
$name = $_POST["name"];

$new_image_name =  print_r($name) . '@' . date('g:i:sa \o\n l, M jS, Y') . '.jpg';

move_uploaded_file($_FILES["file"]["tmp_name"], "wp-content/photos/test/".$new_image_name);

?>

And this view:

<ion-view view-title="Share">
<ion-content>

<p><button ng-click="getPhoto()" class="button button-block button-dark"><i class="icon ion-ios-camera"></i> <b> Take Photo</b></button></p>

<p><button ng-click="getLibrary()" class="button button-block button-dark"><i class="icon ion-ios-camera"></i> <b> Grab from Library</b></button></p>

<p><button ng-click="uploadPhoto()" class="button button-block button-dark"><i class="icon ion-share"></i> <b> Share Photo</b></button></p>

<center><img ng-src="{{lastPhoto}}" style="width: 100%; height: auto; overflow: scroll;"></div></center>

</ion-content>    
</ion-view>

All of which works splendidly, besides the fact that my params only seem to send the number 1 to the server. Sigh…

Do you perhaps see anything I’ve obviously gotten wrong here that would cause such an issue?


#8

Also, Xcode output for a successful file upload looks like this:

Response = Array
(
    [file] => Array
    (
        [name] => cdv_photo_001.jpg
        [type] => image/jpeg
        [tmp_name] => /tmp/phpthq3HL
        [error] => 0
        [size] => 21859
    )

)
test

Everything inside the array makes sense but “test” (which is coming from params.name = “test”) is outside the array. I’m not sure what that means.

Perhaps “1” is an error code for params not found, and maybe it’s not found because params are sent outside the file array and unattached to any identifier?

And perhaps the entire issue is in my PHP, since clearly the array is generated by $_FILE and doesn’t appear to have support for params (http://php.net/manual/en/reserved.variables.files.php)?

Are you using $_FILE or some other method?


#9

Wow I just solved my problem and it was such a simple mistake!

I changed:

<?php 
define('TIMEZONE', 'America/New_York');
date_default_timezone_set(TIMEZONE);
print_r($_FILES);
$name = $_POST["name"];
$dude = $_POST["dude"];

$new_image_name =  print_r($name) . '&' . print_r($dude) . '@' . date('g:i:sa \o\n l, M jS, Y') . '.jpg';

move_uploaded_file($_FILES["file"]["tmp_name"], "test/".$new_image_name);

?>

into

<?php 
define('TIMEZONE', 'America/New_York');
date_default_timezone_set(TIMEZONE);
print_r($_FILES);
$name = $_POST["name"];
$dude = $_POST["dude"];

$new_image_name =  $name . '&' . $dude . '@' . date('g:i:sa \o\n l, M jS, Y') . '.jpg';

move_uploaded_file($_FILES["file"]["tmp_name"], "test/".$new_image_name);

?>

Hopefully this post will save someone else a little time in the future!

Thanks again for your help.


#10

Hey man, delighted to hear you got it working, its always a great feeling. Sorry I wasn’t able to spot the error, good luck with your application :slight_smile: