This program is used to automatic connection to remote sftp server use php and read remote folder, then delete all the files inside that folder.
// First need to connect to remote sftp server use ssh2 module of php (not installed default)
$connection = ssh2_connect('hostname.com', 22);
// Authentication by username and password
ssh2_auth_password($connection, 'username', 'password');
// Sftp
$sftp = ssh2_sftp($connection);
// Remote dir
$remote_dir = "ssh2.sftp://$sftp/path/to/folder";
if(is_dir($remote_dir)) {
if($dh = opendir($remote_dir)) {
while (($file = readdir($dh)) !== false) {
$filetype = filetype($remote_idr . $file);
if($filetype == 'file') { // if is file then remove
unlink ($remote_dir . $file);
}
}
}
}
?>
This program can be used recursive to delete all the files inside the folder use php function filetype of the file name, if file type is dir and file name not equal to '.' or '..', then recursive invoke the program to delete all the files.
No comments:
Post a Comment