Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

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, fileman and listfiles objects

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

  1. antonbiz

    antonbiz Member

    Joined:
    Dec 5, 2015
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    cPanel Access Level:
    Website Owner
    Hello. I am wracking my brain trying to use this with the API2 Fileman listfiles query results. Basically, what I am trying to do is get a list of files in a directory and delete the ones older than a week ago. (its part of a backup solution). So I am after the attributes file, mtime and ctime.

    In the code snip below, I am able to successfully get the value by using $list->data->file, but it only returns the name of one file. There are multiple files in that directory and I need to get each filename to put it away in an array. If I try to go through each item one by one, it returns nothing. I checked the type and they are all objects so I guess I am doing something wrong.

    Below is my code.

    Code:
    
          $list = $xmlapi->api2_query($account, 'Fileman', 'listfiles', $args);
    
          $files = array();
    
            foreach ($list as $item) {
                array_push($files, $item->data->file);
            }
    
           array_push($files, $list->data->file);
    
            print_r($files);
    
            print "Count: " . count($files) . "\r\n";
    
    
            print "Count: " . count($list) . "\r\n";
    
    The output I am getting is

    Code:
    X-Powered-By: PHP/5.5.30
    Content-type: text/html
    
    Array
    (
        [0] =>
        [1] =>
        [2] =>
        [3] =>
        [4] =>
        [5] =>
        [6] =>
        [7] =>
        [8] =>
        [9] => SimpleXMLElement Object
            (
                [0] => backup-12.2.2015_19-50-33_atlas514.tar.gz
            )
    
    )
    Count: 10
    Count: 9
    
    What am I doing wrong?
     
  2. antonbiz

    antonbiz Member

    Joined:
    Dec 5, 2015
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    cPanel Access Level:
    Website Owner
    never mind. answered by own questions. the code to get it is

    Code:
            foreach ($list->data as $item) {
                array_push($files, $item->file);
            }
    
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,127
    Likes Received:
    1,366
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello :)

    I am happy to see you were able to find the solution. Thank you for updating us with the outcome.
     
Loading...

Share This Page