首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用google-drive-api创建子文件夹,并使用PHP返回id

使用google-drive-api创建子文件夹,并使用PHP返回id
EN

Stack Overflow用户
提问于 2013-11-09 08:26:37
回答 3查看 5.4K关注 0票数 6

我可以用下面的代码来创建子文件夹,但是我不能返回id。

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

目前,下面的代码创建了文件夹,但只返回"//“作为id!

谢谢你的帮忙

代码语言:javascript
复制
<?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;
}
?>
EN

回答 3

Stack Overflow用户

发布于 2013-11-09 08:36:09

也许这不是一种花哨的方式,但这是我获取文件夹ID的方法:

代码语言:javascript
复制
$service = new Google_DriveService($client);

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

Stack Overflow用户

发布于 2015-03-28 02:59:38

代码语言:javascript
复制
$files = $service->files->listFiles(array('maxResults' => 1));
var_dump($files['items'][0]['id']); 
票数 0
EN

Stack Overflow用户

发布于 2015-07-22 16:11:26

我认为方法已经更新了,所以我使用了新的方法。Here is the library

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19870859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档