Dropbox API: List files only (including those in subfolders)

3.4k views Asked by At

I am trying to list all files in a specific Dropbox folder, including the files in subfolders, but without displaying the folders themselves.

I am using the DropboxClient PHP Class, but am not attached to it.

My current code works only for files in the first subfolders. Plus it takes a very long time to load due to me querying Dropbox about each folder's content independently. I know I could get a one-time recursive API call, but I seem to fail at working with the Array.

<?php
$folders = $dropbox->GetFiles("/Downloads/",false);

if(!empty($folders)) {

    $i=0;

    foreach($folders as $o) {
        if ($o->is_dir = true) {

            list(, $foldername) = explode('-', $o->path, 2);

            $i++;
            $cat[$i] = $foldername;
            echo '<h2>'.$foldername.'</h2>';

            $files = $dropbox->GetFiles($o->path,false);
            ?>




                <table>
                <tbody><tr>
                    <th>File</th>
                    <th>Date</th>
                    <th>Title</th>
                    <th>Description</th>
                </tr>
                <?php foreach($files as $f): ?>

                <?php

                        $filelink = $dropbox->GetLink($f, false);

                        $filetype = pathinfo($f->path, PATHINFO_EXTENSION);
                        $filename = pathinfo($f->path, PATHINFO_FILENAME);
                        $filenamesplit = explode ( '_-_', $filename);
                ?>

                <tr>
                    <td><a href="<?= $filelink ?>" target="_blank">Download</a></td>
                    <td><?= date("d.m.Y", strtotime($f->modified)); ?></td>
                    <td><?= $filenamesplit[0] ?></td>
                    <td><?= $filenamesplit[1] ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody></table>


            <?php

        }
    }


}

The file structure is e.g. something like this:

Cat1Folder - File 1
Cat1Folder - SubFolder - File 2
Cat1Folder - SubFolder - File 3
Cat2Folder - File 1
Cat2Folder - SubFolder - File 2

And I want a pure list of files to be outputted, regardless of the folder they're in.

Cat1Folder:
File 1
File 2
File 3

Cat2Folder:
File 1
File 2
2

There are 2 answers

0
Raffobaffo On BEST ANSWER

I think you're not longer interested, but while I was looking for something similar I found this from you. From "DropboxClient PHP Class" documentation :

GetFiles($dropbox_path=”, $recursive=false) Get file list of a folder. Returns an array where keys the file paths and values metadata. If $recursive is true, all sub-folders are recursivly scanned. Note that this can take some time because each sub-folder causes a new API request.

So I think you just change your code to: $dropbox->GetFiles("/Downloads/",true);

0
Greg On

The delta call is a more efficient way to get the entire state of the Dropbox, as opposed to calling metadata for every folder. The delta endpoint will returns pages of metadata entries that collectively contain all files.