0
votes

I'm creating a folder (New Folder) inside a given one (FooFolder). If I use "writer" as permission role everything is ok.

My Drive
   |->FooFolder
       |->NewFolder

I have this three functions:

function fileCreation($service, $fileName, $emailToOwn, $type )
{
    //Create the file
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle($fileName);
    $file->setMimeType($type);

    $file = $service->files->insert( $file );

    $permission = insertPermission($service, $file->getId(),$emailToOwn,"user","writer");
    return $file;
}

function insertPermission($service, $fileId, $value, $type, $role) {
  $newPermission = new Google_Service_Drive_Permission();
  $newPermission->setValue($value);
  $newPermission->setType($type);
  $newPermission->setRole($role);

    return $service->permissions->insert($fileId, $newPermission, array(NULL,false));
}

function insertFileIntoFolder($service, $folderId, $fileId) {
  $newChild = new Google_Service_Drive_ChildReference();
  $newChild->setId($fileId);
  try {
    return $service->children->insert($folderId, $newChild);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return false;
}

When I change permission role to "Owner" the new folder is both created correctly in the parent directory and displayed in the root folder.

A link to a picture of my drive after the call of $service->permissions->insert($fileId, "owner");

A link to a picture of my drive after the call of $service->permissions->insert($fileId, "writer");

1
I am not able to get complete understanding of your issue. Could you explain a little bit more clearly. Thanks!KRR
I think an image is a better way to show you the problem. As you can see on the links I just added above If I use "writer" as a permission everything works fine. If I use "owner" all the folders in my "New Folder" are repeated in the root (I use drive in italian so the root is "I miei file")Fabio Malizia

1 Answers

0
votes

I found a solution

It worked to remove the parent folder from each folder I create (I got the code from Drive-Api documentation)

/**
 * Remove a root from a folder.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $folderId ID of the folder to remove the file from.
 * @param String $fileId ID of the file to remove from the folder.
 */
function removeRootFromFolder($service,  $fileId) {
  try {
     $parents = $service->parents->listParents($fileId);
     foreach ($parents->getItems() as $parent) {
       if ($parent->getIsRoot())
       {
            $service->parents->delete($fileId, $parent->getId());
       }
     }

  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}

and updated the method to put a file in a folder

/**
 * Insert a file into a folder.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $folderId ID of the folder to insert the file into.
 * @param String $fileId ID of the file to insert.
 * @return Google_Service_Drive_ChildReference The inserted child. NULL is
 *     returned if an API error occurred.
 */
function insertFileIntoFolder($service, $folderId, $fileId) {
  $newChild = new Google_Service_Drive_ChildReference();
  $newChild->setId($fileId);
  try {
    $r = $service->children->insert($folderId, $newChild);
    removeRootFromFolder($service,  $fileId);
    return $r;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return false;
}