Note that you must enter the full wrapper URI order for functions that accept those parameters to work properly. For example, this (which is referenced more than once in other comments) does not work:
while (false !== ($file = readdir($handle))) if (is_dir($file)) { /* ... */ }
This does work:
$sc = ssh2_sftp(...);
while (false !== ($file = readdir($handle))) if (is_dir("ssh2.sftp://$sc/$file")) { /* ... */ }
You need to pass the "path" into these functions as an fopen() wrapper URI.