Skip to content

Instantly share code, notes, and snippets.

@ridvanbaluyos
Last active March 1, 2021 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ridvanbaluyos/2b353d562bb1f25136c56ff3630f8dca to your computer and use it in GitHub Desktop.
Save ridvanbaluyos/2b353d562bb1f25136c56ff3630f8dca to your computer and use it in GitHub Desktop.
This script recursively loops through all the files in a Google Drive folder.
<?php
/**
* This function recursively gets the Google Drive files.
*
* @param $id
* @return array
*/
function getGoogleDriveFiles($id)
{
$files = openGoogleDriveFolder($id);
$data = [];
// Means it is a file already, return the id.
if (count($files['items']) === 0) {
return $id;
}
// If items there are items, loop through it.
foreach ($files['items'] as $file) {
$fileId = getGoogleDriveFiles($file['id']);
$info['id'] = $fileId;
$info['title'] = $file['title'];
$data[] = $info;
}
return $data;
}
/**
* This function opens a Google Drive Folder.
*
* @param $id
* @return bool|mixed|string
*/
function openGoogleDriveFolder($id)
{
$apiKey = ''; // Your Google API Key
$format = 'https://www.googleapis.com/drive/v2/files?q="%s"+in+parents&key=%s';
$url = sprintf($format, $id, $apiKey);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result, true);
return $result;
}
$driveId = ''; // The google drive folder ID.
$files = getGoogleDriveFiles($driveId);
var_dump($files);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment