't Bijstere spoor

't Bijstere spoor

A blog about Web development

Traversing directories

A few days ago, I came across a situation where I needed to traverse a multi-level directory structure and operate on all the files.

This can be a bit of a tedious job, as you have to deal with either recursion (nice) or a stack (nicer). I had a feeling there might be something in SPL to take on this task, so I took a peek and ended up with this gem:

<?php
  $files 
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));

  foreach(
$files as $file) {

    
// Do your stuff here, $file is an SplFileInfo object

  
}

?>

So how does this work?

I'm going to assume you already know what an Iterator is, if not.. search the interweb.

The DirectoryIterator class can be used to traverse directories, much like scandir() does. The RecursiveDirectoryIterator works the same way, except it implements the hasChildren() and getChildren() methods. hasChildren returns true if the current item has sub-items (in this case, if the item is a directory) and getChildren will return a new iterator for the current subdirectory.

Using this system, we would still need some recursive function to actually go and loop through each subdirectory, this is where RecursiveIteratorIterator comes in. If you pass the RecursiveIteratorIterator any RecursiveIterator object (like RecursiveDirectoryIterator), it will traverse the entire tree for you and pass you all the leaf-nodes.

The leaf nodes are in this context the files. If you also want to make directories part of the traversal, you can change this by passing the constant RecursiveIteratorIterator::SELF_FIRST as the mode parameter.

<?php
  $files 
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'),RecursiveIteratorIterator::SELF_FIRST);
?>

SabreAMF 1.1 release

A new SabreAMF release is up for grabs at the Google Code site.

This is a bugfix-only release, fixing a few issues:

  • The AMF0 and AMF3 serializers did a check with is_numeric to find out if a variable is a number, this would cause problems for numeric-strings, as this function returns true there too. This has been changed to use is_float and is_int.
  • There was a bug with the serialization of integers in AMF3 within certain ranges. The algorithm has been fixed by Kevin Martin.. Also, the current implementation is faster. (thanks kevin! get a site, so I can link you..).
  • When onGetRemoteClass returns an incorrect value (something else than false or a string) an incorrect exception would be thrown, this is fixed as well.
  • Coding standards tid-bits

Strange call from Red Hat

Today I was sitting at my desk when my cell started ringing.

Really odd, a lady from Red Hat called. After she confirms my name, she asks me if I use the RPM packaging system. Unsuspecting, I answer: "ehh, no.. We're a debian shop..". The lady answers "thank you" and hangs up the phone, leaving me wondering how they got my number, if it was really Red Hat and why she would take the time to call me and ask me this seemingly random question.

Should I be worried?


SabreAMF now on gentoo portage

Just came across a Gentoo package for SabreAMF.

I don't have any experience with gentoo, but I think this means if you want to install SabreAMF on a gentoo box, you can simply hit:

emerge SabreAMF

sweet =)