ucantblamem

Archive for September, 2007

GPL v2 and v3 explanation and comparison

30th Sep 2007

Today while surfing around I stumbled across a very comprehensive document outlining not only the licenses themselves, but also the differences between them.

I’ve only really become aware of the importance of licenses (and the knowledge of them) in the last year or so - as you’re probably well aware if you read this blog much. So, I recommend any developer who doesn’t have a really good understanding of licensing and in particular the GNU/GPL, to read this document!

$_FILES Superglobal fix for multiple file uploads

27th Sep 2007

There comes a time in every developers career when they are given the task to create a multiple file upload system. “Okay” you say, “Too easy!”.

If you have a set number of file-input fields to put on the page, you might simply name each like so: “myfile_upload_1″, “myfile_upload_2″, etc… But, if you want to give your site visitors the option to select how many fields to display or you love working with arrays (like myself), you may do something more like this: “myfile_upload[]“.

As stated, I’m a big fan of the second method (the array version); Not only does it give me a nice little array to work with, but if the client sees the four fields I’ve put in the new form and suddenly decides we need a fifth, it’s no sweat at all. Essentially, I’m making life easier for myself if the spec changes (which it almost invariably does).

There is one sour-plum in our punch however and it lies within the $_FILES Super global. Going back to our first illustration of the file-input names: “myfile_upload_1″; On submission of the form, we would expect $_FILES to be populated like so:

Array
{
	[myfile_upload_1] => Array
	{
		[name] => myfile.txt
		[type] => text/plain
		[tmp_name] => /tmp/php/php9GHPab
		[error] => 0
		[size] => 1
	}
}

Pretty straight forward, nothing out of the ordinary. So, what about our second example? Well, this is where things get a little messy:

Array
{
	[myfile_upload] => Array
{
		[name] => Array
		{
			[0] => myfile.txt
		}
		[type] => Array
		{
			[0] => text/plain
		}
		[tmp_name] => Array
		{
			[0] => /tmp/php/php9GHPab
		}
		[error] => Array
		{
			[0] => 0
		}
		[size] => Array
		{
			[0] => 1
		}
	}
}

Can you see what’s going on? It’s pretty hairy. What I would have expected is something more like this:

Array
{
	[myfile_upload] => Array
	{
		[0] => Array
		{
			[name] => myfile.txt
			[type] => text/plain
			[tmp_name] => /tmp/php/php9GHPab
			[error] => 0
			[size] => 1
		}
	}
}

A lot neater, I think you’ll agree! It’s also much more inline with the way that multidimensional arrays normally work in PHP, yet for some reason this issue has gone un-noticed (or at least un-changed) for 5 versions of PHP now.

I must admit, that at times I can be a little stubborn and in regards to this particular problem, I’d really like the $_FILES super global to be predictable (as I’m sure anyone would). So, what can be done? Well, it just so happens that I’ve dealt with this bad-boy before and I put together a function that often finds its’ way into the components we develop at work:

/**
 * PHP $_FILES array builds in an augmented fashion when uploading images from array-style input fields
 * i.e. <input type="file" name="myuploads[]” />
 * This function repairs the $_FILES array so it is more usable
 *
 * @param $array The array of uploaded file information. $_FILES is expected
 * @return array
 */
function repairFileUpload($array){
	$return = array();
	if (is_array($array)) {
		foreach($array AS $field => $val) {
			if (is_array($array[$field]['name'])) {
				foreach(array_keys($array[$field]['name']) AS $key) {
					$return[$field][$key]['name'] = strtolower($array[$field]['name'][$key]);
					$return[$field][$key]['type'] = $array[$field]['type'][$key];
					$return[$field][$key]['size'] = $array[$field]['size'][$key];
					$return[$field][$key]['tmp_name'] = $array[$field]['tmp_name'][$key];
					$return[$field][$key]['error'] = $array[$field]['error'][$key];
				}
			} else {
				$key = 0;
				$return[$field][$key]['name'] = strtolower($array[$field]['name']);
				$return[$field][$key]['type'] = $array[$field]['type'];
				$return[$field][$key]['size'] = $array[$field]['size'];
				$return[$field][$key]['tmp_name'] = $array[$field]['tmp_name'];
				$return[$field][$key]['error'] = $array[$field]['error'];
			}
		}
	}
	return $return;
} // repairFileUpload()

It’s a little “thicker” than I’d like it to be, but it fixes the issue and that’s the important part! Obviously, you simply pass the $_FILES super global to this function fairly early on and you can either re-populate $_FILES:

$files = repairFileUpload($_FILES);
$_FILES = $files;

Or use $files throughout your project. Either way, I hope this snippet of code finds you well and soothes your aching head (if you’ve been beating it against a wall like I did before this function).

Back me up scotty

24th Sep 2007

After a recent evaluation of our current systems, we have been doing quite a few infrastructure updates in preparation for three new work-mates and the launch of some high-traffic websites. As part of these updates, we realised that our development rack has a serious lack of backup power. In fact, a couple of weeks ago we accidentally tripped the buildings’ breaker and our Windows Server 2003 box hadn’t even shut-down before the UPS ran out of juice.

In light of this, my Technical Manager introduced me to APC’s UPS selector; A neat little web-application which helps you figure out how many volt-amps of power you’ll need to run your specified hardware for a specified amount of time.

After putting in all the details of the hardware you want to run, the application gives you a few options, listing the volt-amp rating of each UPS. From this you can get a rough idea of the size of UPS you’ll need and then check out other brands and retailers to get the best price.

Unfortunately, the amount of power our development rack requires is fairly substantial and as a result the cost of the recommended UPS gave us quite a shock (no pun intended). But it was interesting to play around with the tool and see some of the ridiculous products (check out the photos) APC have on offer.