28 Dec

multiple file uploads using HTML5

As a response to a reported bug where Chrome was taking ages to load up a flash multiple-file uploader, I’ve updated KFM to use HTML5’s multiple-file input box where possible.

To do this, first create the element:

  var input=document.createElement('input');
  input.type='file'; // use old-style JavaScript method to make sure all browsers respect it
  input.name='kfm_file';

Notice that we’re not using setAttribute to set the type and name – that’s a DOM method which works in most browsers but not (of course…) in Internet Explorer 6, where it has bugs.

And now, we tell the input to use the multiple-upload method. We use .setAttribute in this case because we only expect newer browsers to succeed with it.

  input.setAttribute('multiple','multiple');
  if(input.multiple)input.name='kfm_file[]';

In the second line, we check to see if the element is now marked as a multiple-uploader (most current browsers will not succeed in this), and if it does, then rename the input element by adding a [] to the end. If this is not done, then the server will only see the first file which is uploaded.

That’s the client-side done. This will only be visible in newer browsers such as Chrome, Safari 4, Firefox 3.6. I expect Internet Explorer will eventually catch up by 2020 or so.

If you’re doing this in pure HTML, then I suppose this would be good enough for you:

<input type="file" multiple="multiple" name="kfm_file[]"/>

In this case, you must put the [] in the name in all cases.

On the server-side, you need to write your upload receiver to expect either a single element, or an array.

For some really goddamned stupid reason, when multiple files are uploaded to PHP, the results are interlaced in a really crappy and awkward manner (I don’t like it).

Instead of something logical and easy to use, like this:

array(
  [0] => array(
    'name'     => 'file1.txt',
    'tmp_name' => '/tmp/abcdef'
    ....
  ),
  [1] => array(
    'name'     => 'file2.txt',
    'tmp_name' => '/tmp/ghijkl'
    ....
  )
);

You get this…

array(
  'name' => array(
    [0] => 'file1.txt',
    [1] => 'file2.txt'
  ),
  'tmp_name' => array(
    [0] => '/tmp/abcdef',
    [1] => '/tmp/ghijkl'
  ),
  ...
);

While that looks at first glance to be easy to use, it’s not. You can’t do a simple “foreach($_FILES['kfm_file'] as $file)” and expect the above to be usable at all…

So, the first thing I do, is to check for the $_FILES[‘kfm_file’], and convert it into the first form above, which is very easy to work with:

$files=array();
$fdata=$_FILES['kfm_file'];
if(is_array($fdata['name'])){
 for($i=0;$i<count($fdata['name']);++$i){
  $files[]=array(
   'name'     => $fdata['name'][$i],
   'tmp_name' => $fdata['tmp_name'][$i],
  );
 }
}
else $files[]=$fdata;

In my own case, I’m only interested in the name and tmp_name variables, so that’s all I set up.

Now you can do a foreach on $files and treat them all individually.

foreach ($files as $file) {
  // uploaded location of file is $file['tmp_name']
  // original filename of file is $file['name']
}

If you want to see this in KFM, have a look at the nightly-updated demo tomorrow, or download from SVN right now.

oh – and buy my book!

17 Dec

novelrank

Because it’s difficult to know exactly how well my book is doing, I went looking for online apps that might be able to help.

I came across NovelRank a few weeks ago, which keeps track of your Amazon SalesRank and uses the fluctuations in the value to try figure out when a sale happens.

At first I was a bit disappointed, as my own ratings should not many sales going on, but I realised that this was because there were no reviews out there so people a) didn’t know about the book, and b) didn’t know if it was worth buying.

Since the reviews have started coming in, sales have picked up, as can be seen in the NovelRank graph for my book.

I like this application – it’s a simple idea, and the author has made it freely available (I assume he makes money from affiliate links).

Want to see it in action? Buy JQuery 1.3 with PHP and then view NovelRank graph for my book the graph a few hours later to see your very own blip appear on it.

It’s interesting to see that the book is not selling at all in Canada. What’s wrong with you Canadians?? ๐Ÿ˜‰

On a very related note, I’m in talks with Packt to produce another book. More on this later when details are more concrete.

13 Dec

jQuery 1.3 With PHP; buy it!

Christmas is coming, as most of us (especially parents and people that have wallets) know, so it’s time for ye all to dig deep and buy the perfect gift for that favourite web developer in your life.

Not knowing what that perfect gift could possibly be, I’ll recommend instead that you invest in a copy of my book, JQuery 1.3 with PHP.

Reviews are just starting to come in. I only know of two on-line ones so far, my favourite of which is this one:

The author does a great job of introducing complicated theories and breaking them down into manageable steps, whilst always taking very thorough considerations for applying jQuery knowledge into CMS รขโ‚ฌโ„ขs and web applications.

I noticed that the same reviewer posted this on Twitter: thoroughly impressed with reading jQuery 1.3 with PHP. writing a review on it, will be available soon!

The other one that I’m aware of is more of a list of notes than a review. The only thing he says in general about the book is “Overall a good book.”

There are a few minor criticisms in the second review that I don’t agree with – that I didn’t use inline functions in all cases, didn’t use Google’s functions to load jQuery from its CDN, and used document.getElementById in some cases instead of using jQuery’s $ function.

My reaction to those points are: inline functions are explained later in the book as I didn’t want to throw the reader head-first into understanding them, there’s no point in loading three libraries (google, jquery and jquery-ui) when you only need jquery and jquery-ui, and for the purposes of getting an element by its ID, document.getElementByID is much quicker than $.

I think the real problem with my decisions with the above points is that, after having had them pointed out as mistakes, I feel I should really have explained more clearly in the book why I chose to do things in those ways in the first place. Well, that’s something for edition 2 ๐Ÿ˜€

So far, though, the reactions are positive, and I hope this continues – there haven’t been any “this is crap” reviews so far, which is good.

I know of a few other people that are writing reviews, and can’t wait to see them. So reviewers, please do criticise it – it makes the end-product better.

And christmas shoppers, it’s a great book ๐Ÿ˜‰

07 Dec

php.ie slowly upgrading

It’s been a while since I wrote anything vaguely technical. I guess it’s because I like to write only when there’s something new to say, and usually only if I have some new code to give away.

No new code today, but I can describe the recent work on php.ie (I’m the secretary of the Irish PHP Users’ Group).

So firstly, it was basically a static/brochure site for about a year, until we installed WebME (written by me!) as the CMS and created a skin for it so there’s only a tiny design difference. If you want to try out WebME, then download the SVN version from the google code site, or create a test site here (uses a really old version of WebME – you’re better off using the SVN version).

Then, I started rewriting the right panel. Beforehand, it displayed recent twitter messages, but they’re not often put out so it was a bit of a wasted space.

The panel now uses a WebME widget which displays recent Twitter messages, emails from the mailing list, and posts from the forum.

Over the next few days, I’ll be adding a new News section to the site, and the message widget will be able to show new articles from planet php.ie and new jobs from the jobs page.

I’m currently reading through Ken’s linux.ie todo list to see what I can appropriate for php.ie for its ongoing development.

Big thanks go to Michele and the team at blacknight for hosting the site.

Oh! Just a reminder, buy my book! JQuery 1.3 with PHP – hasn’t been reviewed by anyone yet, as far as I know, but my own opinion is that it is worth having on your shelf if you are a PHP developer that wants to step into jQuery.

01 Dec

Graham Coxon at the Barbican

Bronwyn and myself went to the Barbican, London, on Saturday to watch Graham Coxon perform.

We both enjoyed the event. Bronwyn was excited to meet friends she had only spoken to online. Well, she’s been excited for the whole of the last week, but it’s all related!

London is big.

The weather was ok for the Friday and Saturday while we were wandering around taking in sights and sounds. We visited the National Gallery, and were handed a sheet saying a candle-lit baroque concert would be happening later, but it clashed with our previous plans.

Arrived at the Barbican. Bronwyn didn’t see any of her friends. We said we’d meet up around the bar, so that’s where we went, and sat opposite it.

We were there about five minutes when I spotted a huge amazing monstrosity of a drum-machine, Felix’s Machines. You have to see the videos of that thing!

As I stood there, Simon from Resigned (also the admin of the Graham Coxon forum) noticed me and waved to get my attention – ah, that’s where they are! We joined a group of Coxon fans.

We had two hours, so we gently infused ourselves thanks to the bar, with some opting for chips and complaining that you shouldn’t need to buy fish&chips just to get some chips (as a vegetarian, I agree wholeheartedly with this, and not just through a hatred of waste).

The show was to start at 8, so we headed down and got our seats.

Simon had thoughtfully gotten us row G (haha – G for Graham. very good. ahem…), which had a walk-space directly in front of us, meaning we could stretch our legs and walk to the toilets without stepping on people’s heads.

Bronwyn decided a new piece of policy was to be created henceforth: when purchasing tickets, people should be measured for height, and really tall people should be confined to the back of the auditorium.

The band came out and the place became loud with cheers.

The sound engineers didn’t do the best job in the world. The band played brilliantly apart from a few minor hiccups, but some of the sound problems were distracting.

When Graham spoke, it was difficult to hear. I was afraid that his singing would be the same, but when he sings, he crouches close to the microphone, and when he talks, it’s like he’s unaware the mic is there.

Some of the songs were technical, involving a lot of finger-picking. An example is Sorrow’s Army. Graham started out on that one, then Robyn Hitchcock joined in a few bars later. Robyn’s guitar, though, was louder, so it drowned out Graham’s playing. This was pointed out independently to me by Simon later on, so it wasn’t just my ears playing tricks.

There was a feedback problem later on at the beginning of one tune, which was quickly and cheerfully quelled and restarted.

One of the three female singers was very loud at points. I didn’t like that – it was like she was stealing the spotlight.

On the far left of the stage, Max Eastley was playing the Arc. At most points in the concert I couldn’t hear anything of what he was doing. Only in quiet songs with only one or two other instruments.

When the songs got loud, they got very loud. Graham was unintelligible at some points as he tried to sing above the sound of the other instruments.

Apart from these gripes (and they’re minor – Bronwyn doesn’t agree with any of the above points), I enjoyed the concert.

I think the only tune I didn’t like was the ending of Caspian Sea, where the band appeared to get stuck in a rut, repeating the same bar over and over and over.

I liked how the music was not perfectly in-tune or perfectly rhythmic, but was just a little off here and there. This gave the music a more natural and “used” feel, like an old rickety piano which is played when the pianist is surrounded by friends – you feel like he’s playing personally to you and it’s not a surgical procedure.

The concert was basically Graham’s latest album, The Spinning Top, with a few extra old songs played at the end.

One of the things I like about this album is the finger-picking. Graham has recently been trying to increase his finger-picking skills, inspired by his love of old blues and folk. His interest in Nick Drake really shines through in the singing, and Bert Jansch (of Pentangle) in the playing.

In a lot of the songs, there is not just one finger-picking “voice”, but two. This could be seen obviously at the concert where Graham was playing one finger-picking riff and Robyn was playing another, yet they meshed nicely.

Overall, I enjoyed this concert and if he does it again with another album, I’m sure we’ll be going over again.