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

如何用PHP cURL通过Simple Upload将文件传至Google Drive并移至指定文件夹

PHP cURL 上传Google Drive文件后重命名并移动文件夹问题

已完成操作

  1. 完成OAuth验证,成功将本地文件上传至Google Drive根目录,代码如下:
$image = "../../../".$name;
$apiURL = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media';
$mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
$folder_id = "1WBkQQ6y0TPt2gmFR3PKCzSip_aAuuNEa";

$ch1 = curl_init();    
curl_setopt($ch1, CURLOPT_URL, $apiURL);
curl_setopt($ch1, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, file_get_contents($image));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: '.$mime_type, 'Authorization: Bearer ' . $access_token) );
$response=curl_exec($ch1);
if($response === false){
    $output = 'ERROR: '.curl_error($ch1);
} else{
    $output = $response;
}
curl_close($ch1);
$this_response_arr = json_decode($response, true);
  1. 成功将默认名称为“Untitled”的文件重命名,代码如下:
if(isset($this_response_arr['id'])){
    $this_file_id = $this_response_arr['id'];
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_file_id);
    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'PATCH');
    $post_fields = array();
    $this_file_name = explode('.', $name);
    $post_fields['name'] = $this_file_name[0];
    curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode($post_fields));
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
    $response2 = curl_exec($ch2);
    if($response2 === false){
        $output2 = 'ERROR: '.curl_error($ch2);
    } else{
        $output2 = $response2;
    }
   
    curl_close($ch2);
    $this_response2 = json_decode($response2, true);
}

问题:无法将文件移动到指定文件夹

尝试在重命名请求体中添加Parents/addParents/removeParents参数,或单独发起PATCH请求移动文件均失败,尝试的移动代码如下:

if($this_response2['id']){
    $this_f_id = $this_response2['id'];
    $ch3 = curl_init();
    curl_setopt($ch3, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_f_id);
    curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'PATCH');
    curl_setopt($ch3, CURLOPT_POST, 1);
    $post_fields1 = array();
    $post_fields1['addParents'] = $folder_id;
    $post_fields1['removeParents'] = "root";
    curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($post_fields1));
    curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
    $response3 = curl_exec($ch3);
    if($response3 === false){
        $output3 = 'ERROR: '.curl_error($ch3);
    } else{
        $output3 = $response3;
    }
    curl_close($ch3);
}

解决方法

1. 修正cURL请求参数冲突

移动代码中同时设置CURLOPT_CUSTOMREQUEST, 'PATCH'和CURLOPT_POST, 1会导致请求方法混乱,需移除CURLOPT_POST, 1的设置。

2. 检查权限

确保OAuth access_token拥有https://www.googleapis.com/auth/drive或https://www.googleapis.com/auth/drive.file权限(前者权限覆盖范围广,后者仅允许操作应用创建的文件)。

3. 修正后的移动文件代码

if(isset($this_response2['id'])){
    $this_f_id = $this_response2['id'];
    $ch3 = curl_init();
    curl_setopt($ch3, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_f_id);
    curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'PATCH');
    $post_fields1 = array(
        'addParents' => $folder_id,
        'removeParents' => 'root'
    );
    curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($post_fields1));
    curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch3, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $access_token
    ));
    $response3 = curl_exec($ch3);
    
    // 打印响应排查错误
    if($response3 === false){
        $output3 = 'ERROR: '.curl_error($ch3);
        echo $output3;
    } else{
        $response3_arr = json_decode($response3, true);
        if(isset($response3_arr['error'])){
            echo 'API ERROR: ' . $response3_arr['error']['message'];
        } else{
            $output3 = '文件移动成功';
            echo $output3;
        }
    }
    curl_close($ch3);
}

4. 优化操作:合并重命名与移动请求

可将重命名和移动操作合并为一个PATCH请求,减少API调用次数:

if(isset($this_response_arr['id'])){
    $this_file_id = $this_response_arr['id'];
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_file_id);
    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'PATCH');
    $this_file_name = explode('.', $name);
    $post_fields = array(
        'name' => $this_file_name[0],
        'addParents' => $folder_id,
        'removeParents' => 'root'
    );
    curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode($post_fields));
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
    $response2 = curl_exec($ch2);
    if($response2 === false){
        $output2 = 'ERROR: '.curl_error($ch2);
    } else{
        $response2_arr = json_decode($response2, true);
        if(isset($response2_arr['error'])){
            $output2 = 'API ERROR: ' . $response2_arr['error']['message'];
        } else{
            $output2 = '文件重命名并移动成功';
        }
    }
    curl_close($ch2);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 01:40:34