I am working on a website project. I have created an application on Google Developers Console and installed Google SDK on my server. Authorization done, offline access done, listing all files/dirs done, creating files done.
Now I would like to list files/dirs which is located in root directory. I mean only those not all child directories.
here is code sample:
// $client is a Google_Client object
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "trashed=false"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
echo "Dosya Adı: ".$file->getName()."<br>";
echo "Dosya Id: ".$file->getId()."<br>";
echo "Tip: ".$file->getMimeType()."<br>";
echo "Dosya Link: ".$file->getWebContentLink()."<br>";
echo "Dosya Link: ".$file->getWebViewLink()."<br>";
}
}
this code listing all files/dirs (not in trash). i added "'root' in parents" to q but its give me nothing. didnt find anything useful in documentation.
here is the output:
Files:
Dosya Adı: 1.jpg
Dosya Id: **some-id**
Tip: image/jpeg
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk
Dosya Adı: images2
Dosya Id: **some-id**
Tip: application/vnd.google-apps.folder
Dosya Link:
Dosya Link: https://docs.google.com/folderview?id=**some-id**&usp=drivesdk
Dosya Adı: images
Dosya Id: **some-id**
Tip: application/vnd.google-apps.folder
Dosya Link:
Dosya Link: https://docs.google.com/folderview?id=**some-id**&usp=drivesdk
Dosya Adı: sample_01.jpg
Dosya Id: **some-id**
Tip: image/jpeg
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk
Dosya Adı: 1302.pdf
Dosya Id: **some-id**
Tip: application/pdf
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk
You can use "root" as an alias for your root directory ID:
$driveService->files->listFiles([
// 'q' => '"'.MAIN_FOLDER_ID.'" in parents', // optionnal
'q' => '"root" in parents', // optionnal
'pageSize' => 100, // 1 to 1000
'fields' => 'nextPageToken, files(id, name, mimeType, parents)'
]);
I have found the solution finally
// $client is a Google_Client object
function rootFolderId($client) {
$service = new Google_Service_Drive($client);
$results = $service->files->get('root');
return $results->getId();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With