Cordova Filetransfer to PHP server.

Now this has taken me so long to figure out, I almost gave up so many times but hey I eventually slew the dragon. I went to so many different solutions and they only took me so far then I got stuck.

Issue:

I needed to upload photos stored on my Android device to a PHP server, using Cordova File Transfer plugin.

Technology:

  1. Cordova: 3.6
  2. Android 4.0.3
  3. File Transfer Plugin

JavaScript:

function uploadPhoto(fileURL) {
	var options = new FileUploadOptions();
	options.fileKey = "file";
	options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
	options.mimeType = "text/plain";

	var params = {};
	params.value1 = "test";
	params.value2 = "param";

	options.params = params;

	var ft = new FileTransfer();
	ft.upload(fileURL, encodeURI("http://yourdomain.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("error");
		   switch (error.code) {
		     case FileTransferError.FILE_NOT_FOUND_ERR:
		        alert("Photo file not found");
		        break;
		     case FileTransferError.INVALID_URL_ERR:
		       alert("Bad Photo URL");
		       break;
		     case FileTransferError.CONNECTION_ERR:
		       alert("Connection error");
		       break;
		   }
		   alert("An error has occurred: Code = " + error.code);
	}

PHP Code:

 if ($_FILES["file"]["error"] > 0) {
  echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
$new_image_name = strtolower($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_image_name);
}

Special Notes:

Make sure you have an upload folder, also this code will use the name of the file stored on the Android Device.

This solution worked for me if you have any questions I will try to answer the best I can.

Advertisements

Codeigniter has a new home

I have been using Codeigniter for about a year now. To be honest I was concerned when I first started using it that it was about to die. But I could not find a PHP that was so simple, did the job and got my work out quicker. But I through caution to the wind and dove into it even though there were issues with its final ownership.

Today I found out it has been taken over here is a link to the article at Ellislab

https://ellislab.com/blog/entry/your-favorite-php-framework-codeigniter-has-a-new-home

So I will continue to use and support it.

Javascript Objects and Pods Framework

I am a lot better at JavaScript than PHP, just one of those things. I absolutely love Pods Framework for WordPress, it has really taken my Custom WordPress work to another level. But recently while working on the African Securities Exchanges website i was faced with a page that required Stock exchange figures for a few years that needed graphs with a lot of interactivity. I had created a separate Pod for member exchanges to capture their yearly statistics.

The solution was quite simple I used the PODS template system to create the JSON data for me here is how I did it.

First create the template in Pods.

{ member : "{@member.name}", year : "{@year}" , etc... },

as you can see his will create a JavaScript Object, and it loops through all the members and creates a new object for each member and year. Notice the comma after the object, this is important for the array of objects.

Create the JavaScript Array in WordPress Page Template

var monthly_statistics = [ <!--?php pods_content(); ?> {}]; // This gets an array from monthly statistics by member
monthly_statistics.pop(); // Remove last object from array

So I assign the pods content to the array, I had to add an extra object near the end to make sure the array gets assigned correctly to the variable, I then removed the last Object from the array by using pop().

Now you can assign to id or classes on page

jQuery("#market_capitalization").html(monthly_statistics[0].market_capitalization);

I am sure there is a more elegant way of doing this but for now this is working for me, also can easily add each or for loops to objects.

 

Me be normal

Well things have been going well, I have signed my first client from the United Kingdom, so we can now earn some foreign revenue.

I am always amazed how the world has changed, most of my clients I have never met in person, and some I have never even spoken to on the phone, we have a strictly email working relationship. My closest client is 100Km from where I work, the world has changed, good or bad it’s all good.

Bootstrap

I have been amazed that my attitude to scripting has really changed. For years I thought it was the goal to be able to do all the scripting myself. Being it HTML, CSS or JavaScript. I always saw it as a bit of cheating when I used a framework, felt less like a developer for some reason. Maybe it is because I am a bit older, but hey I could not care a damn any more. Whatever technology, framework that makes my life easier I am all for it. I have tried a few but have now settled on Bootstrap another development company in my Area Country Digital introduced me to it and I have not looked back. I use it for standard HTML, PHP and WordPress Development. It has lessened the development time for each project, and the more we use it the faster we are becoming.

Now I do custom WordPress themes, something I would not have tried if not for Bootstrap, I would like to get more to grips with LESS as I think it will speed things up even more, but slowly, slowly as they say.

Update

I am always amazed how quickly I can forget to write something here, I have been very busy with work and the likes so just a quick update on what is going on.

I finished a private share trading platform to sell shares and debentures in Bridge. The site was completely designed and scripted by myself, it uses PHP and MySQL have a quick look http://bridgeregister.coombeinc.co.za, you can’t really see much as it requires a username and password.

I am still working on the Website for I-Net Bridge I think this is really going to be one of my Flagship Custom WordPress websites, It is still in production but you can see the work in progress here http://asea.inet.co.za

I have also been doing some specialist scripting for Clark Kent, creating a Used Car section for their client SMG (Land Rover, Jaguar and Toyota)

So work has really been quite productive.

I few months back I made the decision to focus on fewer technologies to follow and learn, my main focus is JavaScript. I have now started learning Node.JS this is back end JavaScript, or JavaScript on a Server. It is amazing how it has opened so many doors in my head on ways forward, I would highly recommend Node.JS it is fast and very easy to set up and learn, well that’s my 2 cents in any case.

Well Have a wonderful week.