4

I tried to use phpseclib to delete all logs in SFTP server.

Codes are simple:

$sftp = new Net_SFTP($host_name);
$sftp->login($username, $password); // login is successful
$sftp->chdir('/somefolder');
if(!$sftp->delete('*.log')) {
  $logger->error('Cannot remove logs');
}

The log shows "Cannot remove logs".

However, I use SFTP command in shell, it works :

$ sftp myusername@example.com
Password: (type in my password)
sftp> cd /somefolder
sftp> rm *.log
Removing xxx.log
Removing yyy.log
sftp> ls
( no more *.log )
sftp> exit

Does the phpseclib delete function supports wildcard character ? If not, any alternatives ?

Raptor
  • 53,206
  • 45
  • 230
  • 366

1 Answers1

2
Does the phpseclib delete function supports wildcard character ? If not, any alternatives ?

Not at present no. I guess you could do $sftp->nlist() and do a preg_match on each row returned by nlist. If it matches delete it, otherwise keep it.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • sad that it does not mention in its example. – Raptor Aug 20 '13 at 04:31
  • I think it's unreasonable to have that expectation? Does ftp_delete support wildcards? Does ssh2_sftp_unlink? Does unlink? Neither http://php.net/ssh2-sftp-unlink pr http://php.net/ftp-delete or http://php.net/unlink make mention of that.. – neubert Aug 20 '13 at 12:41
  • No. The website mentions it fully implements SFTP features, so I assume they have support on wildcards. – Raptor Aug 21 '13 at 02:22
  • Where does it say it fully implements SFTP's features? http://phpseclib.sourceforge.net/sftp/intro.html doesn't say that. And in any event, wildcards arguably aren't even a feature of SFTP the protocol so much as they're a feature of sftp the program. Maybe you could create a ticket on github.com suggesting that as a feature but I still think you're expectations are unreasonable and unfounded. – neubert Aug 21 '13 at 02:51