You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google共享驱动器文件移动函数返回200但文件未移动的问题

问题:Google Drive共享驱动器文件移动函数返回200但文件未实际移动

我写了一个在Google共享驱动器中移动文件的PHP函数,执行后返回HTTP 200状态码,没有报错,但文件根本没被移动。函数代码如下:

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;
}

调试信息如下:

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
)

所有参数都正确,我拥有共享驱动器的管理员全权限,请问怎么解决?


解决方法

问题出在两个核心细节上,都是Google Drive API的隐性要求:

1. addParents和removeParents必须传数组类型

Google Drive API明确要求这两个字段接收字符串数组,哪怕只有一个文件夹ID。你当前传的是单个字符串,API会直接忽略这个参数,导致执行了空操作(所以返回200但没实际修改)。

修改$move_data部分:

$move_data = [
    'addParents' => [$target_folder_id],
    'removeParents' => [$source_folder_id],
];

2. 移除URL中多余的corpora参数

corpora是用于文件列表查询(比如list接口)的参数,在修改文件的PATCH请求里完全不需要。多余的参数可能导致API内部处理逻辑异常,隐性忽略修改请求。

修改API URL:

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

3. 可选:添加fields参数验证修改结果

可以在URL里加上fields=parents,id,name,让API返回文件当前的父文件夹ID,方便直接验证移动是否成功:

$apiURL = self::DRIVE_FILES_URI . $file_id . "?includeItemsFromAllDrives=true&supportsAllDrives=true&fields=parents,id,name";

修改后的完整函数代码:

public function MoveFileToFolder($access_token, $file_id, $source_folder_id, $target_folder_id) {
    $apiURL = self::DRIVE_FILES_URI . $file_id . "?includeItemsFromAllDrives=true&supportsAllDrives=true&fields=parents,id,name";

    $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;
}

执行后,返回的$data里会包含parents数组,你可以直接看到文件现在的父文件夹ID,确认移动是否生效。

内容的提问来源于stack exchange,提问作者Ampopo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 22:13:16