PHP 5.4 File Upload Progress and HTML5 Progress Bars

This posts was originally written for using pre release versions of php5.4, now PHP5.4 has been release everything should work as before I haven’t tested it. There is probably more choice in ways to install it now.

There was a live demo of this but I couldn’t justify the cost of a whole ec2 instance just for this!

The way I have implemented this may be problematic in Chrome/Safari due to Webkit Bug 23933,  ”XMLHttpRequest doesn’t work while submitting a form (useful for progress tracking)” I tested this in firefox (the latest version at the time) and it worked fine. Chrome may or may not work. Let me know how you get on.

Full details of the upload progress implementation can be found the official RFC: https://wiki.php.net/rfc/session_upload_progress

Developers who work with PHP applications that upload files commonly struggle with providing user feedback on the upload progress, usually using flash and javascript solutions like uploadify. In PHP 5.4 there is now integrated functionality to allow file upload progress to be passed back to the browser.

In this post I’ll describe the basic operation of this feature and describe a quick example of its use.

How it Works

The upload progress functionality stores the current progress in a session variable which can then be queried as required to give the current progress, it requires the use of PHP native sessions. The $_SESSION key is set by the form name and a prefix defined in php.ini

Read more ▼

Installing PHP 5.4 in Ubuntu

I’ve been excitedly awaiting some of the new features found in PHP 5.4, in particular array notation and file upload progress monitoring so I decided to try and install PHP 5.4 on Ubuntu. Fortunately there is a repository of prebuilt packages for Ubuntu.

I recommend using a VM so you don’t mess up any stable PHP install.

Update

Since writing this post php 5.4 stable has been released and a more up to date package is available in this ppa: https://launchpad.net/~ondrej/+archive/php5 I haven’t tested this personally but should work as described below.

Installation

The ppa can be simply added to ubuntu:

sudo add-apt-repository ppa:ondrej/php5

Then installing as normal

sudo apt-get update
sudo apt-get install php5 libapache2-mod-php5

Codeigniter Unit Testing with Simpletest and PHP 5.3

I’m a regular user of codeigniter-simpletest developed by Eric Barnes however I ran into a number of issues when attempting to run some features under PHP5.3, this was due to using an old version of Simpletest. I have now updated this and had my pull request accepted and merged into the main repo so that it now uses the most recent versions (1.1alpha3).

There is however one important change to note which will stop old tests working, that is the way that test classes define a label, these are optional so this only applies if you use them in your project. The name of the test is now passed through the simpletest constructor otherwise the default is to use the classname of the test.

Before

class test_users_model extends CodeIgniterUnitTestCase
{
	public function __construct()
	{
		parent::__construct();

		$this->UnitTestCase('Users Model');

		$this->load->model('users/users_model');
	}

etc...

After

class test_users_model extends CodeIgniterUnitTestCase
{
	public function __construct()
	{
		parent::__construct('Users Model');

		$this->load->model('users/users_model');
	}
etc...

I’m also working on integration with CI systems, Jenkins in particular based on this rather cool presentation: Setting up a Continuous Integration Server for Ubuntu with Codeigniter and Github

PHP North West 2011

This was my first PHPNW conference but after hearing so many good things about the conference in previous years I thought I’d give it a go. In this post I’ll summarise some of my highlights.

Read more ▼

Speeding up PHP – Using process forking to accelerate image resizing

Most of my projects at work involve resizing images in PHP applications, usually the result is loaded via AJAX so it needs to be generated as fast as possible for optimum user experience. Recently I discovered the ability to fork processes in PHP, this gives the ability to run multiple functions at the same time. In the example below I’ll demonstrate how parallel processing can be used in PHP to speed the generation of several different sizes of preview images from a large uploaded image.

This code uses the PCNTL extension which was installed as standard on my Ubuntu 11.04 PHP 5.3 install.

Read more ▼

MQTT Caller Display – Hacking the BT Caller Display 50 Serial Port

This post describes a recent project I completed to display caller id information for incoming calls on my computers. The project uses a BT Caller Display 50 connected via an optoisolating bridge to an arduino with an ethernet shield. When an incoming call is received the telephone number is transmitted via MQTT to a python client on any computer I happen to be using at the time. This project is a great example of someone putting great effort into being lazy, the caller id data is alredy displayed on my phone but the phone generally resides in the next room so this avoids me needing to run for stupid cold callers. Initially I’d wanted to do this project just using the arduino, which may have been possible but using the CD50 makes it a huge amount easier.

Read more ▼

Simple sitemaps in codeigniter

Creating sitemaps is a bit of a hassle for any site bigger than a couple of pages, especially where a database is involved. While working on a recent project I created a library for codeigniter which allows the simple creation of sitemaps either by manually adding pages which might be appropriate for a blog, or allowing the library to scan controllers and adding all pages defined by methods. Common search engines can also be notified.

A simple example of it in use is shown below:

//if you're using sparks
$this->load->spark('codeigniter-sitemaps/0.0.1')

$this->load->library('sitemaps');

//assuming a hypothetical posts_model
$posts = $this->posts_model->get_posts();

foreach ($posts AS $post)
{
$item = array(
"loc" => site_url("blog/" . $post->slug),
"lastmod" => date("c", strtotime($post->last_modified)),
"changefreq" => "hourly",
"priority" => "0.8"
);

$this->sitemaps->add_item($item);
}

// file name may change due to compression
$file_name = $this->sitemaps->build("sitemap_blog.xml");

$reponses = $this->sitemaps->ping(site_url($file_name));

You can download the spark from: http://getsparks.org/packages/codeigniter-sitemaps

Or get the code from my github: http://github.com/chemicaloliver/codeigniter-sitemaps

Simple documentation is included and should be self explanatory.

This library is based on work by Philipp Dörner 2010 http://signalkraft.com/google-sitemaps-for-codeigniter

Double sided male headers – ease your FTDI cable to breadboard connections woes!

When prototyping circuits I often use a breadboard and an FTDI cable which usually leaves some kind of hacky looking unstable connection using wires stuck into the FTDI cable female header connector. This arrangement often leads to failiure as the wires fall out. After much searching I’ve managed to locate a solution to allow neat connection of a female header to the breadboard.

See below for details:
Read more ▼

Creating a Remote Webcam using NodeJS, Android, Opera Mobile, Web Sockets and HTML5

Recently I’ve been experimenting with using webcams in the browser with Javascript, while I was browsing Google on related topics I came across an interesting video on youtube. As I couldn’t find any code for how the demo had been created I decided to create my own (see below). The idea of streaming video from a phone to a server via a websocket had occurred to me before I viewed this video but I dismissed it as impractical, an opinion which has only been reinforced after testing it myself.

This is the video that gave me the inspiration (my code replicates this exactly so I didn’t see the need to recreate the video too), the only difference is I used linux all round rather than windows:

Read more ▼

Enabling web sockets on Opera Mobile

A little project I’m working on requires the use or websockets in Opera Mobile, I’d read they were supported but when I tried it just threw an exception, then I discovered you have to enable them in the config:

  1. Browse to opera:config in opera mobile
  2. Open the User Prefs section
  3. Check the enable websockets box
  4. Scroll right to the bottom of the page and select save
  5. Enjoy much websockety goodness