The problem is that even using a negative starting position, substr() is still looking to start there and grab $length characters to the
right. Since you are starting at the last character, there is nothing else to grab.
What you want to do is start $length characters in from the right, and grab everything else, like:
substr($filename,-$length);
More docs on substr() can be found
here.
- Matt Riffle