Google drive folder not create inside parent folder (by Folder ID)

1.3k views Asked by At

I upload the files from my site it will upload to google drive inside the child folder via Google Drive API. Everything working fine. But I want to create a folder inside a parent folder. I have parent folder id. But it cannot create inside parent folder. It will create folder outside of parent folder.

I have tried with below code:

        $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

There are 2 answers

0
Vel On BEST ANSWER

This code work for.

        $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();
        }
0
some1 On

it should be

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

instead of

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