I am looking into it a little more but for starters does dpm() show the correct value for `$dir`? Also, according to the [documentation](https://api.drupal.org/api/drupal/includes%21file.inc/function/file_scan_directory/7) the trailing slash on `$dir` is not supposed to be there.