I'm trying to look for specific files on an SFTP server using PHP. The file listing is quite large, so I'm hoping to filter the results using wildcards rather than iterating through every file, if possible.
When I use either psftp
or even the default Windows command line sftp
command, I'm able to filter the results using something like ls prefix_*.txt
or whatever, and the files returned are, as expected, filtered by the filemask I used.
However, phpseclib3\Net\SFTP does not seem to have an ls
command, just nlist
and rawlist
. Both of which offer the option to do sorting, but don't seem to work with wildcards.
I also tried this, thinking perhaps the SSH command would work:
$sftp->exec('ls');
but the server quits with 'This service allows sftp connections only.'
So my question is whether that is possible in PHP, or I need to just accept that I'll have to loop through all of the returned files every time I want to filter them. If that's the case, how does the ls
command in those other tools work? Is the filtering handled locally in the psftp
and Windows sftp
applications (i.e. they still need to retrieve a full directory listing behind the scenes)?
The filtering in sftp
and psftp
is done locally. In SFTP protocol, there's no API to do the filtering server-side.
The phpseclib does not have local-side filtering implemented. You indeed have to do it on your own.