API2 and delete files
Hi, I am having some issues deleting old backup of a site I created. Below is my code.
Could anyone give me a hint where I am going wrong. Everything works except the unlink function. Basically, I am creating an array list of files in backup folder and if the file is older than 5 days delete it.
If there is a better solution via cron, I am totally open to ideas.
password_auth($account, $password);
$xmlapi->set_port(2083);
$xmlapi->set_debug(1);
$args = array(
'checkleaf' => '1',
'dir' => '/backup',
'filelist' => '0',
'filepath-*' => '',
'needmime' => '0',
'showdotfiles' => '0',
'types' => 'file',
);
$list = $xmlapi->api2_query($account, 'Fileman', 'listfiles', $args);
$files = array();
$delete_date = strtotime("-5 days");
foreach ($list->data as $item) {
$files[] = array($item->file, $item->mtime, $item->ctime, $item->fullpath, $item->humansize);
}
foreach ($files as $file) {
if ($file[1] < $delete_date) {
$args2 = array(
'op' => 'unlink',
'sourcefiles' => $file[0],
'doubledecode' => 0
);
$xmlapi = new xmlapi($server);
$xmlapi->password_auth($account, $password);
$xmlapi->set_port(2083);
return $xmlapi->api2_query('Fileman', 'fileop', $args2);
}
}
?>-
I think I answered my question again. The API2 code seems to be return $xmlapi->api2_query($account, 'Fileman', 'fileop', $args2);
Once I added the account it started deleting old files. I think someone should update the API docs.0 -
Hello :) I am happy to see you were able to address the issue. Could you let us know which specific document is not providing the correct information? Thank you. 0 -
Hi. So I am basically following this doc. cPanel API 2 Functions - Fileman::fileop - Software Development Kit - cPanel Documentation Maybe I misread it or I am not using the function the same way. But nowhere does it say you need to specify the account before Fileman. 0 -
Please keep in mind that cPanel API 2 is deprecated. You may want to consider switching to UAPI: UAPI Functions - Fileman::list_files - Software Development Kit - cPanel Documentation Some functionality may not yet exist, but you could likely develop code that manually handles certain file operations (e.g. generate a list of files to delete or unlink and handle it through a cron job). Thank you. 0
Please sign in to leave a comment.
Comments
4 comments