Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

php - search folder for any subfolder with name specified by me and get its id

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]=>
question from:https://stackoverflow.com/questions/65869705/search-folder-for-any-subfolder-with-name-specified-by-me-and-get-its-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe your goal and the current situation as follows.

  • You want to check whether the folder of $folderName is existing in the specific folder of $mainfolderId.
  • When the folder of $folderName is existing, you want to retrieve the folder ID.
  • When the folder of $folderName is NOT existing, you want to create new folder with the folder name of $folderName and retrieve the folder ID.
  • You want to achieve this using Drive API v3 with googleapis for PHP.
  • You have already been able to retrieve the file metadata using Drive API.

Modification points:

  • In your script,
    • $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.
    • The script for checking whether the folder of $folderName is existing in the specific folder is required to be added.
    • The script for creating new folder is required to be added.

When above points are reflected to a script, it becomes as follows.

Sample script:

$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);
  • In this modified script, the script for retrieving $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
  • When above script is used, when the folder of $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.

Note:

  • In this case, it supposes that your $service can be used for creating new folder. Please be careful this.
  • If an error related to the scopes, please use the scope of https://www.googleapis.com/auth/drive.

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...