Question:
How to move a file between folders on Google Shared Drives?

Problem:

I wrote the following function to move a file between folders in the Google shared drives. It works normally, without any error, but the file is not moved. How can I do?

public function MoveFileToFolder($access_token, $file_id, $source_folder_id, $target_folder_id) {

        $apiURL = self::DRIVE_FILES_URI . $file_id . "?corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true";


        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $apiURL);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));


        $move_data = [

            'addParents' => $target_folder_id,

            'removeParents' => $source_folder_id,

        ];


        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($move_data));


        $data = json_decode(curl_exec($ch), true);


        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);


        if ($http_code != 200) {

            $error_msg = 'Failed to move file to folder';

            if (curl_errno($ch)) {

                $error_msg = curl_error($ch);

            }

            throw new Exception('Error ' . $http_code . ': ' . $error_msg);

        }


        return $data;

    }


I would move the file, debug shows

File_id: 1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG

Source_parent_id: 12QqaPU5DEqGDO6csMy-kjZYS0dmKJfW7

Target_parent_id: 1D-2_ZDYGRDgXgHAfK9nOQqzfYBo3fXIb

Query: https://www.googleapis.com/drive/v3/files/1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG?corpora=allDrives&includeItemsFromAllDrives=true&supportsAllDrives=true

Response HTTP Code: 200

Response Data: Array

(

    [kind] => drive#file

    [id] => 1vN8eC8XolWEc0OtpFUY2PBewfh5O9VqG

    [name] => Firme presenze anno 2023 Accoglienza maschile Settembre IACUITTO DANIELE.pdf

    [mimeType] => application/pdf

    [teamDriveId] => 0AB8c-2VqWymEUk9PVA

    [driveId] => 0AB8c-2VqWymEUk9PVA

)


how the function works normally, but the file is not moved. All parameters are correct, I'm administrator with full privileges


Solution:

This is the correct function:

public function MoveFileToFolder($access_token, $file_id, $source_folder_id, $target_folder_id) {

    $apiURL = self::DRIVE_FILES_URI . $file_id . "?addParents=$target_folder_id&removeParents=$source_folder_id&supportsAllDrives=true";


    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $apiURL);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access_token));


    $data = json_decode(curl_exec($ch), true);


    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);


    if ($http_code != 200) {

        $error_msg = 'Failed to move file to folder';

        if (curl_errno($ch)) {

            $error_msg = curl_error($ch);

        }

        throw new Exception('Error ' . $http_code . ': ' . $error_msg);

    }


    return $data["driveId"];

}


Answered by: >Ampopo

Credit: >Stack Overflow


Read more:

>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>Use of Singletons in .NET Core in AWS Lambda

>How to create a line animation in Python?

>Migrate From Haruko To AWS App: 5 Simple Steps

>How you can create array with associative array in other array php?


Ritu Singh

Ritu Singh

Submit
0 Answers