Last active
March 1, 2021 22:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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