Google驱动器文件夹无法在父文件夹中创建(按文件夹ID)

时间:2016-12-28 12:32:40

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

我从我的网站上传文件,它将通过Google Drive API上传到子文件夹中的google驱动器。一切正常。但我想在父文件夹中创建一个文件夹。我有父文件夹ID。但它无法在父文件夹中创建。它将在父文件夹之外创建文件夹。

我尝试使用以下代码:

        $ParentfolderId="0Bxy_x723UM3tFFFSSSWWW";           

        $DriveFolderName = "foldername1";
        $folder_mime = "application/vnd.google-apps.folder";
        $folder_name = $DriveFolderName;
        $folder = new Google_Service_Drive_DriveFile(array('parents' => array( $ParentfolderId)));
        $folder->setTitle($folder_name);
        $folder->setDescription($GoogleDriveDesc); 
        $folder->setMimeType($folder_mime);             
        $newFolder = $service->files->insert($folder);
        $folderId  = $newFolder['id'];

        $file = new Google_Service_Drive_DriveFile();

        if ($folderId != null) {
            $parent = new Google_Service_Drive_ParentReference();
            $parent->setId($folderId);          
            $file->setParents(array($parent));
        } 
        try {
            $data = file_get_contents($filepath);           
            $file->title = $filename;       
            $chunkSizeBytes = 1 * 1024 * 1024;

            $client->setDefer(true);
            $request = $service->files->insert($file);

            $media = new Google_Http_MediaFileUpload(
                  $client,
                  $request,
                  '',
                  null,
                  true,
                  $chunkSizeBytes
            );

            $media->setFileSize(filesize($filepath));
            $status = false;
            $handle = fopen($filepath, "rb");
            while (!$status && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $status = $media->nextChunk($chunk);
            }

            $result = false;
            if ($status != false) {
                $result = $status;
            }
            return $result;

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

2 个答案:

答案 0 :(得分:0)

应该是

$folder = new Google_Service_Drive_DriveFile(array('parents' => array($ParentfolderId)));

而不是

$folder = new Google_Service_Drive_DriveFile(array('parents' => array($DriveFolderName)));

答案 1 :(得分:0)

此代码适用于。

        $ParentfolderId="0Bxy_x723UM3tFFFSSSWWW";           

        $DriveFolderName = "foldername1";
        $folder_mime = "application/vnd.google-apps.folder";
        $folder_name = $DriveFolderName;
        $folder = new Google_Service_Drive_DriveFile(array('parents' => array( $ParentfolderId)));
        $folder->setTitle($folder_name);
        $folder->setDescription($GoogleDriveDesc); 
        $folder->setMimeType($folder_mime);     

        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId($ParentfolderId);
        $folder->setParents(array($parent));

        $newFolder = $service->files->insert($folder);
        $folderId  = $newFolder['id'];

        $file = new Google_Service_Drive_DriveFile();

        if ($folderId != null) {
            $parent = new Google_Service_Drive_ParentReference();
            $parent->setId($folderId);          
            $file->setParents(array($parent));
        } 
        try {
            $data = file_get_contents($filepath);           
            $file->title = $filename;       
            $chunkSizeBytes = 1 * 1024 * 1024;

            $client->setDefer(true);
            $request = $service->files->insert($file);

            $media = new Google_Http_MediaFileUpload(
                  $client,
                  $request,
                  '',
                  null,
                  true,
                  $chunkSizeBytes
            );

            $media->setFileSize(filesize($filepath));
            $status = false;
            $handle = fopen($filepath, "rb");
            while (!$status && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $status = $media->nextChunk($chunk);
            }

            $result = false;
            if ($status != false) {
                $result = $status;
            }
            return $result;

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