[英]Create a sub folder using google-drive-api and return the id with PHP
我可以使用下面的代碼創建子文件夾,但我只是無法返回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;
}
?>
也許這不是花哨的方式,但這是獲取文件夾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;
}
}
$files = $service->files->listFiles(array('maxResults' => 1));
var_dump($files['items'][0]['id']);
我認為方法已經更新,所以我使用了新方法。 這是圖書館
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.