Home > Mobile >  How to move a uploaded file to a specific drive folder in using Simple Upload in Php cURL?
How to move a uploaded file to a specific drive folder in using Simple Upload in Php cURL?

Time:01-06

My requirement is to upload a file to my google drive using Php cURL and then rename the upload file and move the file to a specific folder.

For this, I have done oAuth and successfully uploaded a file from my website to google drive using the below code.

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

The file is uploaded as Untitled and I used the below code to rename it to a proper filename as per my requirement.

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

Now I want to move this uploaded file in the Google drive root folder to a specific folder. I tried using “Parents” , “addParents”, “removeParents” parameters but none of them is working.

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

}

Any help would be appreciated.

CodePudding user response:

Documentation for uploading,renaming and moving the file in gdrive is not properly documented for Php cURL and there are not much examples available too.

There is no need to send an additional cURL request for moving the file to a specific folder. This can be done in the second cURL request itself.

The mistake in your code is, you are sending the addParents and removeParents in Request Body instead of sending this as query parameters.

You can modify the second cURL as below to update the name of uploaded file and for moving the file inside a specific folder.

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

Let me know if this works.

  • Related