Google驱动PHP API:无法将文件或文件夹插入子文件夹 [英] Google drive PHP API: unable to insert files or folders into subfolders

查看:185
本文介绍了Google驱动PHP API:无法将文件或文件夹插入子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎一段时间,通过Google驱动器PHP API,我可以创建一个子文件夹或将文件添加到现有文件夹,但尝试在子文件夹中放置另一个子文件夹或文件似乎是不可能的。

经过研究,我发现了儿童功能,但不明白如何应用它,即使在查看此页面上的Google文档之后:[ https://developers.google.com/drive/v2/reference/children/insert ]



我用来将图片添加到文件夹的代码是:

  //将文件插入客户端特定文件夹
$ file = new Google_Service_Drive_DriveFile();
$ file-> setTitle(uniqid()。'。jpg');
$ file-> setDescription('测试文档');
$ file-> setMimeType('image / jpeg');

$ data = file_get_contents('a.jpg');

$ parent = new Google_Service_Drive_ParentReference(); // previous Google_ParentReference
$ parent-> setId($ folderid); // $ folderid =确定的文件夹ID
$ file-> setParents(array($ parent));
$ createdFile = $ service-> files-> insert($ file,array(
'data'=> $ data,
'mimeType'=>'image / jpeg ',
'uploadType'=>'multipart'
));

如何继续将此图片上传到特定的子文件夹,其中的ID已经确定?

更新:
这是我的组合代码尝试:



  1. 使用existing_folder的父项创建new_sub_folder1,并存储返回的ID

  2. 使用步骤1中存储的ID创建new_sub1文件夹的父文件夹new_file1
  3. >




  $ service = new Google_Service_Drive($ client); 

//创建子文件夹
$ folder = new Google_Service_Drive_DriveFile();
//设置文件夹以创建
$ folder-> setTitle('new_sub_folder1');
$ folder-> setMimeType('application / vnd.google-apps.folder');
//在existing_folder中创建文件夹
$ parentid ='0B40CySVsd_Jaa1BzVUQwLUFyODA';
//将父文件夹设置为existing_folder
$ parent = new Google_Service_Drive_ParentReference(); // previous Google_ParentReference
$ parent-> setId($ parentid);
$ folder-> setParents(array($ parent));

//现在创建客户端特定文件夹new_sub_folder
try {
$ createdFile = $ service-> files-> insert($ folder,array(
'mimeType'=>'application / vnd.google-apps.folder',
));
//返回创建文件夹的ID
$ subfolderid = $ createdFile-> id;
echo $ subfolderid;
返回$ createdFile-> id;
} catch(Exception $ e){
print发生错误:。 $ E->的getMessage();
}

//将文件插入客户端特定文件夹new_sub_folder
$ file = new Google_Service_Drive_DriveFile();
$ file-> setTitle(uniqid()。'。jpg');
$ file-> setDescription('测试文档');
$ file-> setMimeType('image / jpeg');

$ data = file_get_contents('a.jpg');


//将父文件夹设置为new_sub_folder
$ parent = new Google_Service_Drive_ParentReference(); // previous Google_ParentReference
$ parent-> setId($ subfolderid);
$ file-> setParents(array($ parent));
$ createdFile = $ service-> files-> insert($ file,array(
'data'=> $ data,
'mimeType'=>'image / jpeg ',
'uploadType'=>'multipart'
));

print_r($ createdFile);

但是只有new_sub_folder1被创建,没有文件被添加到这个文件夹中。



更新:
使用此代码不会将图像添加到任何位置。如果我使用相同的方法将.jpg添加到existing_folder,则可以通过其ID来确定没有问题。只要我使用sub_folder_1的ID,就不会创建任何东西 - 相同的方法,不同的ID。

解决方案

将文件夹视为培养。你的文件(在这种情况下是一个文件夹)必须连接到其他两个火车车才能工作。前一个是父母,下一个是孩子。如果你指定一个父母,但不是一个文件夹的孩子,那么火车是不完整的。有时候火车并不完全没关系,但是如果你想使用至少两层文件,那么你必须使用父母和孩子的引用。



你的基本文件夹可能具有作为其父文件夹的根目录,则此文件夹可能是另一个子文件夹的父文件夹,但如果您计划使用多个层次层次结构以获得完整引用,则需要指定它具有子项。因此,请检查以下代码:

  $ foldID = $ id; 
$ folder4 = new Google_Service_Drive_DriveFile();
$ folder4-> setTitle($ folderName);
$ folder4-> setMimeType('application / vnd.google-apps.folder');
$ parent4 = new Google_Service_Drive_ParentReference();
$ parent4-> setId($ foldID);
$ folder4-> setParents(array($ parent4));
try {
$ createdFile4 = $ service-> files-> insert($ folder4,array(
'mimeType'=>'application / vnd.google-apps.folder' ,
));
}
catch(Exception $ e){
print发生错误:。 $ E->的getMessage();
}
$ foldId4 = $ createdFile4-> id;
$ newChild4 =新的Google_Service_Drive_ChildReference();
$ newChild4-> setId($ createdFile4-> id);
尝试{
$ service-> children->插入($ foldID,$ newChild4);
}
catch(Exception $ e){
print发生错误:。 $ E->的getMessage();

$ / code>

关于代码的一些事情:
foldID是我的一种主文件夹或根文件夹。我创建一个新的DriveFile,指定文件夹规范,为我的主文件夹指定一个父引用,尝试插入它,然后用children-> insert(我的主文件夹的ID,创建的文件夹的ID)指定一个子引用。现在参考文件已完成,并且子文件夹应该如此处理。对于另一个子子文件夹,只需冲洗并使用正确的参数重复。我有一个大约6或7级文件夹和文件的目录结构,所以它是可能的,所以它只是为了正确索引文件。


I have been struggling for quite a while now; via Google drives PHP API, I am able to create a sub folder or add files to an existing folder, But trying to place another sub folder or a file within a sub folder, seems impossible.

After research, I came across the Children function, but don't understand how to apply it, even after checking the Google documentation on this page: [https://developers.google.com/drive/v2/reference/children/insert][1]

The code I am using to add an image to a folder is:

//Insert a file into client specific folder
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');

$data = file_get_contents('a.jpg');

$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($folderid); //$folderid = determined folder id 
$file->setParents(array($parent));  
$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'image/jpeg',
      'uploadType' => 'multipart'
    ));

How would I proceed to upload this image to a specific subfolder, of which the ID is already determined?

Update: This is my combined code trying to:

  1. Create new_sub_folder1 with a parent of existing_folder, and store the returned ID
  2. Create new_file1 with a parent of new_sub_folder1, using the ID stored in step 1

$service = new Google_Service_Drive($client);

//create sub folder
$folder = new Google_Service_Drive_DriveFile();
//Setup the folder to create
$folder->setTitle('new_sub_folder1');
$folder->setMimeType('application/vnd.google-apps.folder');
//Create the Folder within existing_folder
$parentid = '0B40CySVsd_Jaa1BzVUQwLUFyODA';
//Set the Parent Folder to existing_folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($parentid);
$folder->setParents(array($parent));

//now create the client specific folder  new_sub_folder
try {
        $createdFile = $service->files->insert($folder, array(
            'mimeType' => 'application/vnd.google-apps.folder',
            ));
        // Return the created folder's id
        $subfolderid = $createdFile->id;
        echo $subfolderid;
return $createdFile->id;
} catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
}

//Insert a file into client specific folder new_sub_folder
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');

$data = file_get_contents('a.jpg');


//Set the Parent Folder to new_sub_folder
$parent = new Google_Service_Drive_ParentReference(); //previously Google_ParentReference
$parent->setId($subfolderid);
$file->setParents(array($parent));  
$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'image/jpeg',
      'uploadType' => 'multipart'
    ));

print_r($createdFile);

But only new_sub_folder1 is created, and no file is added to this folder.

Update: The image is not added anywhere with this code. If I apply the same method to add .jpg to the existing_folder, by its ID, there are no issues. As soon as I use the sub_folder_1's ID, nothing is created - same method, different ID.

解决方案

Think of the folders as a train. Where your file (in this case a folder) has to be attached to 2 other train carts in order to work. The previous one is the parent and the next one is the child. If you specify a parent, but not a child for a folder then the train is incomplete. Sometimes it doesn't matter that the train is not complete, but if you want to use at least 2 levels of files then you must use parent and children references.

Your base folder may have root as its parent, then this folder could be the father of another sub-folder but it needs to be specified that it has children if you plan to use several level hierarchy for it to have a complete reference. So check this code:

$foldID=$id;    
$folder4=new Google_Service_Drive_DriveFile();
        $folder4->setTitle($folderName);
        $folder4->setMimeType('application/vnd.google-apps.folder');
        $parent4=new Google_Service_Drive_ParentReference();
        $parent4->setId($foldID);
        $folder4->setParents(array($parent4));
        try {
            $createdFile4 = $service->files->insert($folder4, array(
                'mimeType' => 'application/vnd.google-apps.folder',
            ));                 
        } 
        catch (Exception $e) {
                    print "An error occurred: " . $e->getMessage();
        }
        $foldId4=$createdFile4->id;
        $newChild4=new Google_Service_Drive_ChildReference();
        $newChild4->setId($createdFile4->id);
        try{
            $service->children->insert($foldID,$newChild4); 
        }
        catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        } 

A few things about the code: foldID is my kind of master or root folder. I create a new DriveFile, assign the folder specifications, assign a parent reference of my master folder, try to insert it, then specify a child reference with the children->insert(ID OF MY MASTER FOLDER, ID OF THE CREATED FOLDER). The reference now is complete, and the subfolder should be treated as such. For another sub-sub-folder, just rinse and repeat with the right parameters. I have a directory structure of approximately 6 or 7 level of folders and files, so it is possible so it's just to index the files correctly.

这篇关于Google驱动PHP API:无法将文件或文件夹插入子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆