【问题标题】:Create a sub folder using google-drive-api and return the id with PHP使用 google-drive-api 创建一个子文件夹并使用 PHP 返回 id
【发布时间】:2013-11-09 00:26:36
【问题描述】:

我可以使用下面的代码创建子文件夹,但我无法返回 id。

我想获取文件夹的 ID 和/或链接,以便将它们添加到我的数据库中。

目前以下创建文件夹,但只返回“//”作为 id!

感谢您的帮助

<?php

if ($client->getAccessToken()) {

$file = new Google_DriveFile();

//Setup the Folder to Create
$file->setTitle('Project  Folder');
$file->setDescription('A Project Folder');
$file->setMimeType('application/vnd.google-apps.folder');

//Set the ProjectsFolder Parent
$parent = new Google_ParentReference();
$parent->setId('0B9mYBlahBlahBlah');
$file->setParents(array($parent));

//create the ProjectFolder in the Parent
$createdFile = $service->files->insert($file, array(
    'mimeType' => 'application/vnd.google-apps.folder',
));

// print_r($createdFile);
print "folder created sucessfully";
echo "folder id is" . $createdFile->id;
}
?>

【问题讨论】:

  • 你的问题很有帮助。但是你的方法是错误的。我认为他们已经更新了他们的 api....
  • 我喜欢这个问题,随后调用列表函数确实有效,但不切实际。最好有一种方法,直接从初始调用返回 id,就像在上传带有 uploadType=resumable 的文件时发生的那样

标签: php google-drive-api google-api-php-client


【解决方案1】:

也许这不是花哨的方式,但这是我获取文件夹 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;
    }
}

【讨论】:

    【解决方案2】:

    我认为方法已经更新,所以我使用了新方法。 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;
    }
    

    【讨论】:

      【解决方案3】:
      $files = $service->files->listFiles(array('maxResults' => 1));
      var_dump($files['items'][0]['id']); 
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      相关资源
      最近更新 更多