You can do `$dir = realpath($dir);`. That will remove the trailing slash and avoid repeating the computation many times.