How to import subfolders and files into array using Google Drive

Hello,

With the code below, I list the subfolders and files in the source folder with echo. However, I can’t get this list out as an array. What am I doing wrong? Can you help me?

<?php 
$copy_from_id = '1ayLi94BVtYHvzmbBXmmW3siQrwiJOotc';

function listFiles($service, $folderId, $path = ''): array
{
        $results = $service->files->listFiles([
            'orderBy' => "name",
            'q' => "'$folderId' in parents",
        ]);
    $list_array = [];
    foreach ($results->getFiles() as $file) {

        $filePath = $path . '/' . $file->getName();
        echo $filePath."<br />";

        $list_array[$file->getId()] = $filePath;
        
        if ($file->mimeType == 'application/vnd.google-apps.folder') {

            listFiles($service, $file->getId(), $filePath);

        }
        
    }
    return $list_array;
}

$list_array = listFiles($service, $copy_from_id);

echo '<pre>' . print_r($list_array, true) . '</pre>';
?>

list with echo

/aaaaaaaa
/aaaaaaaa.zip
/bbbbbbb
/bbbbbbb.zip
/zaman
/zaman/bir_alt_zaman
/zaman/bir_alt_zaman/ddddddd.zip
/zaman/bir_alt_zaman/iki_alt_zaman
/zaman/bir_alt_zaman/iki_alt_zaman/web1
/zaman/bir_alt_zaman/iki_alt_zaman/web1/web1.zip
/zaman/bir_alt_zaman/iki_alt_zaman/web2
/zaman/bir_alt_zaman/iki_alt_zaman/web2/web2.zip
/zaman/bir_alt_zaman/iki_alt_zaman/web3
/zaman/bir_alt_zaman/iki_alt_zaman/web3/web3.zip
/zaman/bir_alt_zaman/iki_alt_zaman/web4
/zaman/bir_alt_zaman/iki_alt_zaman/web4/web4.zip
/zaman/bir_alt_zaman/iki_alt_zaman/web5
/zaman/bir_alt_zaman/iki_alt_zaman/web5/web5.zip
/zaman/bir_alt_zaman/iki_alt_zaman/webler.zip
/zaman/cccccccc.zip

The result I got with the array

Array
(
    [1Ck1AqWEX832hDd90sNmjyvddl5RzyEtv] => /aaaaaaaa
    [1Q7qj_Kx5cIsPxWnmLLRnPC5CamkArFPD] => /aaaaaaaa.zip
    [1bJLje66HuCmMhLfbL91M8a59GsxgMe9Y] => /bbbbbbb
    [1o5VJqxv8z_IVaCP7PqPrU3VB4k7PSUlL] => /bbbbbbb.zip
    [1qalXTD6R2JTIgNy98IGB6spf7lwC8x9K] => /zaman
)

This function works

$copy_from_id = '1ayLi94BVtYHvzmbBXmmW3siQrwiJOotc';

function listFiles($service, $folderId, $path = '') {
$resultArray = [];
$results = $service->files->listFiles([
    'orderBy' => "name",
    'q' => "'$folderId' in parents",
]);

foreach ($results->getFiles() as $file) {

    $filePath = $path . '/' . $file->getName();
    $resultArray[$file->getId()] = $filePath;
    
    if ($file->mimeType == 'application/vnd.google-apps.folder') {
        $resultArray = array_merge($resultArray, listFiles($service, $file->getId(), $filePath));
    } // if ($file->mimeType == 'application/vnd.google-apps.folder') {

} // foreach ($results->getFiles() as $file) {

return $resultArray;
} // function listFiles($service, $folderId, $path = '') {


$startingFolderId = ''.$copy_from_id.'';

$filePaths = listFiles($service, $startingFolderId);

echo '<pre>' . print_r($filePaths, true) . '</pre>';

Array output

Array
(
    [1Ck1AqWEX832hDd90sNmjyvddl5RzyEtv] => /aaaaaaaa
    [1Q7qj_Kx5cIsPxWnmLLRnPC5CamkArFPD] => /aaaaaaaa.zip
    [1bJLje66HuCmMhLfbL91M8a59GsxgMe9Y] => /bbbbbbb
    [1o5VJqxv8z_IVaCP7PqPrU3VB4k7PSUlL] => /bbbbbbb.zip
    [1qalXTD6R2JTIgNy98IGB6spf7lwC8x9K] => /zaman
    [1Cdb2aO69HO4t05i1xfv3EzOFAI5TUrBQ] => /zaman/bir_alt_zaman
    [1tykwJ7e08GhzpLqT4APFDRJ8ktTGKywc] => /zaman/bir_alt_zaman/ddddddd.zip
    [1YbbNcKwvAmKUw6Zncd0Q2VVtttHVplDc] => /zaman/bir_alt_zaman/iki_alt_zaman
    [12SNJez-idblIZLT6bv8qlTkYzSU4Jutn] => /zaman/bir_alt_zaman/iki_alt_zaman/web1
    [1sw2jy2Br65vS1j0kXqK0K2n22VTVH8w-] => /zaman/bir_alt_zaman/iki_alt_zaman/web1/web1.zip
    [1vlbq0bYcdVNrcbUKzgjvum2wN0kHc-W3] => /zaman/bir_alt_zaman/iki_alt_zaman/web2
    [1JsbK6JLLh9uJBDj1fdXOWO1hHJxy00i9] => /zaman/bir_alt_zaman/iki_alt_zaman/web2/web2.zip
    [1hSSHA0hgUTW61x0vTOL5cIEV68TIu-Bj] => /zaman/bir_alt_zaman/iki_alt_zaman/web3
    [1j-rT8wou0CBdx3NYEPWIukuZhoUuCHpQ] => /zaman/bir_alt_zaman/iki_alt_zaman/web3/web3.zip
    [1TWpgEuOGtpFlBfhSlKHrxnv6OrQ3kRJ4] => /zaman/bir_alt_zaman/iki_alt_zaman/web4
    [1YRdR9ysO30QzGYUeA1sf5sVyHToWLKfn] => /zaman/bir_alt_zaman/iki_alt_zaman/web4/web4.zip
    [1CNadIEWxeiHVULLlUWGwjJZDHR3olPmc] => /zaman/bir_alt_zaman/iki_alt_zaman/web5
    [1-T0Xs9DgaL9gy-rUPQZS_L102mxaP2GS] => /zaman/bir_alt_zaman/iki_alt_zaman/web5/web5.zip
    [19yekcdLZCmKLsfQ8JxjZifVbEuy1xGsh] => /zaman/bir_alt_zaman/iki_alt_zaman/webler.zip
    [16-IQjDg08RTPvqIOlcYRrueNxD5isEn_] => /zaman/cccccccc.zip
)
Sponsor our Newsletter | Privacy Policy | Terms of Service