Create a sub folder using google-drive-api and return the id with PHP

天大地大妈咪最大 提交于 2019-12-05 06:14:22

Maybe it's not fancy way but this is my method to get folder ID:

$service = new Google_DriveService($client);

$files = $service->files->listFiles();
foreach ($files['items'] as $item) {
    if ($item['title'] == 'your_folder_name') {
        $folderId = $item['id'];
    break;
    }
}

I think methods has been updated so i have used new methods. Here is the library

if ($client->getAccessToken()) {
      $file = new Google_Service_Drive_DriveFile();
      $file->title = "New File By Bikash 111";
      // To create new folder
      $file->setMimeType('application/vnd.google-apps.folder');


      // To set parent folder
      $parent = new Google_Service_Drive_ParentReference();
      $parent->setId('0B5kdKIFfgdfggyTHpEQlpmcExXRW8');
      $file->setParents(array($parent));

      $chunkSizeBytes = 1 * 1024 * 1024;

      // Call the API with the media upload, defer so it doesn't immediately return.
      $client->setDefer(true);
      $request = $service->files->insert($file);

      // Create a media file upload to represent our upload process.
      $media = new Google_Http_MediaFileUpload(
      $client,
      $request,
      'text/plain',
      null,
      true,
      $chunkSizeBytes
      );
      $media->setFileSize(filesize(TESTFILE));

      // Upload the various chunks. $status will be false until the process is
      // complete.
      $status = false;
      $handle = fopen(TESTFILE, "rb");
      while (!$status && !feof($handle)) {
        $chunk = fread($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
      }
      //Here you will get the new created folder's id
      echo "<pre>";
      var_dump($status->id);
      exit;
}
user1629045
$files = $service->files->listFiles(array('maxResults' => 1));
var_dump($files['items'][0]['id']); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!