Reputation: 41
hello I am trying to do a search on a google drive folder, if the subfolder exists then I get the id of the subfolder if not then I need to create it and get its id so i can upload files to it later.
all of the searches will be within the main folder and also i am trying to find if the folder exists based on its name i am using php for this,
so far i have managed using var_dump to get array like this in the image with the id I am trying to find included with some gibberish there maybe if there is a way to clean it up
$mainfolderId = "1CpRAH5PTisqdfsdqfsIAsdfsdoyfuELsVixBtJA"; // main folder where evrthing is saved don't edit
$folderName = "one ya"; // Please set the series folder name here.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files',
'q' => "name = '".$folderName."' and mimeType = 'application/vnd.google-apps.folder' "
);
$results = $service->files->listFiles($optParams);
print_r($results);
and this is what i get ( i have used a tool to make it more pleasent to look at) i also removed the rest be its too much you can also see ["id"]=> string(33) is the one i need
object(Google_Service_Drive_DriveFile)#81 (76) {
["collection_key":protected]=> string(6) "spaces"
["appProperties"]=> NULL
["capabilitiesType":protected]=> string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=> string(0) ""
["contentHintsType":protected]=> string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=> string(0) ""
["contentRestrictionsType":protected]=> string(39) "Google_Service_Drive_ContentRestriction"
["contentRestrictionsDataType":protected]=> string(5) "array"
["copyRequiresWriterPermission"]=> bool(false)
["createdTime"]=> string(24) "2021-01-24T09:22:59.645Z"
["description"]=> NULL
["driveId"]=> NULL
["explicitlyTrashed"]=> bool(false)
["exportLinks"]=> NULL
["fileExtension"]=> NULL
["folderColorRgb"]=> string(7) "#8f8f8f"
["fullFileExtension"]=> NULL
["hasAugmentedPermissions"]=> NULL
["hasThumbnail"]=> bool(false)
["headRevisionId"]=> NULL
["iconLink"]=> string(96) "hisddddddddennn by me "
["id"]=> string(33) "1XAmpgAVPudqFzfRoMAny" <----------- thiiiiiiiisss
["imageMediaMetadataType":protected]=> string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=> string(0) ""
["isAppAuthorized"]=> bool(false)
["kind"]=> string(10) "drive#file"
["lastModifyingUserType":protected]=> string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
Upvotes: 0
Views: 777
Reputation: 201378
I believe your goal and the current situation as follows.
$folderName
is existing in the specific folder of $mainfolderId
.$folderName
is existing, you want to retrieve the folder ID.$folderName
is NOT existing, you want to create new folder with the folder name of $folderName
and retrieve the folder ID.$mainfolderId
is not used. In this case, in order to search the folder in the specific folder, '{$mainfolderId}' in parents
is required to be used.$folderName
is existing in the specific folder is required to be added.When above points are reflected to a script, it becomes as follows.
$mainfolderId = "###"; // Please set the folder ID you want to search folder.
$folderName = "###"; // Please set the folder name you want to search.
$service = new Google_Service_Drive($client);
$folders = $service->files->listFiles(array("q" => "name='{$folderName}' and '{$mainfolderId}' in parents and mimeType='application/vnd.google-apps.folder'"));
$folderId = '';
if (count($folders->getFiles()) == 0) {
$f = new Google_Service_Drive_DriveFile();
$f->setName($folderName);
$f->setMimeType('application/vnd.google-apps.folder');
$f->setParents(array($mainfolderId));
$folderId = $service->files->create($f)->getId();
} else {
$folderId = $folders->getFiles()[0]->getId();
}
print($folderId);
$client
is not shown. Because I thought that from your script and question, the script for authorizing scopes has already been prepared. If you are required to know the script, please check the Quickstart for PHP. Ref$folderName
is existing in the folder of $mainfolderId
, the folder ID of the existing folder is returned. And, when the folder of $folderName
is NOT existing in the folder of $mainfolderId
, new folder is created and the created folder ID is returned.$service
can be used for creating new folder. Please be careful this.https://www.googleapis.com/auth/drive
.Upvotes: 2