php 如何使用简单上传将上传的文件移动到特定的驱动器文件夹中?

hgqdbh6s  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(144)

我的要求是使用Php cURL将文件上传到我的Google Drive,然后重命名上传文件并将文件移动到特定的文件夹。
为此,我已经做了oAuth,并成功地上传了一个文件,从我的网站到谷歌驱动器使用下面的代码.

$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) );
    // execute cURL request
    $response=curl_exec($ch1);
    if($response === false){
        $output = 'ERROR: '.curl_error($ch1);
    } else{
        $output = $response;
    }
    // close first request handler
    curl_close($ch1);
    $this_response_arr = json_decode($response, true);

该文件上传为无标题,我用下面的代码来重命名为一个适当的文件名,根据我的要求。

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

现在我想把Google Drive根文件夹中这个上传的文件移动到特定的文件夹中,我尝试使用“Parents”、“AddParents”、“RemoveParents”参数,但都不起作用。

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

}
任何帮助都将不胜感激。

sy5wg1nm

sy5wg1nm1#

在gdrive中上传、重命名和移动文件的文档对于Php cURL来说没有正确的文档记录,也没有太多的例子。
不需要发送额外的cURL请求来将文件移动到特定的文件夹。这可以在第二个cURL请求本身中完成。
代码中的错误在于,您在请求正文中发送addParents和removeParents,而不是将其作为查询参数发送。
您可以按如下所示修改第二个cURL,以更新上载文件的名称并将文件移动到特定文件夹中。

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.'?addParents='.$folder_id.'&removeParents=root');
    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);
    }

如果管用就告诉我。

相关问题