API2, fileman and listfiles objects

antonbiz

Member
Dec 5, 2015
5
0
1
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?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
Hello :)

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