The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

API2 and delete files

Discussion in 'cPanel Developers' started by antonbiz, Dec 10, 2015.

  1. antonbiz

    antonbiz Member

    Joined:
    Dec 5, 2015
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    cPanel Access Level:
    Website Owner
    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.



    Code:
    <?php
    
        error_reporting(E_ALL);
        ini_set('display_errors', true);
    
        include "xmlapi.php.inc";
        include "config.php.inc";
    
        $xmlapi = new xmlapi($server);
        $xmlapi->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);
    
               }
             }
    
    ?>
     
  2. antonbiz

    antonbiz Member

    Joined:
    Dec 5, 2015
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    cPanel Access Level:
    Website Owner
    I think I answered my question again. The API2 code seems to be

    Code:
    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.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
  4. antonbiz

    antonbiz Member

    Joined:
    Dec 5, 2015
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    cPanel Access Level:
    Website Owner
  5. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page