简体   繁体   中英

How to copy file from google drive to another server using php

I want to copy file from google drive to another server using php.

I have used google drive picker and when user clicks on any file I used to get download url, I was thinking that it can be done through copy function, but it didn't work.

Any solution Please.

function getDownloadurl(fileId) {
           var request =  gapi.client.request({
                    'path': '/drive/v2/files/' + fileId,
                    'params': { 'maxResults': '1000' },
                    callback: function (responsejs, responsetxt) {
                    var fileDownloadUrl = responsejs.downloadUrl; 
              $.ajax({
                type: "POST",
                url: "ajax-files/copy_drive_file.php",
                data: { gd_url:fileDownloadUrl },
                async:false,
                success: function(data)
                {   

                }
});

copy_drive_file.php

$sourcePath=$_POST['gd_url'];
copy($sourcePath, 'sss/file.jpg'); 

You need to authorize and authenticate your requests while fetching from PHP

YourJsFile

function getDownloadurl(fileId) {
var accessToken =  gapi.auth.getToken().access_token;
var request = gapi.client.request({
            'path': '/drive/v2/files/' + fileId,
            'params': {
                'maxResults': '1000'
            },
            callback: function(responsejs, responsetxt) {
                var fileDownloadUrl = responsejs.downloadUrl;
                $.ajax({
                    type: "POST",
                    url: "ajax-files/copy_drive_file.php",
                    data: {
                        gd_url: fileDownloadUrl,
                        accessToken: accessToken
                    },
                    async: false,
                    success: function(data) {
                        console.log(data);
                    }
                });
            }
       });
} 

copy_drive_file.php

if ( isset($_POST['gd_url']) && isset($_POST['accessToken'])) {
    $url = $_POST['gd_url'];
    $accessToken = $_POST['accessToken'];
    $opts = array(
            'http'=>array(
            'method'=>"GET",
            'header' => "Authorization: Bearer " . $accessToken                 
            )
    );
    $context = stream_context_create($opts);
    $content = file_get_contents($url, false, $context);
    if (!empty($content)){
        file_put_contents("path/filename.file_extension",$content);
        echo json_encode(array('file_name' => "filename.file_extension"));
    }
}

Maybe this will help?

function copyFile($service, $originFileId, $copyTitle) {
      $copiedFile = new Google_Service_Drive_DriveFile();
      $copiedFile->setTitle($copyTitle);
      try {
        return $service->files->copy($originFileId, $copiedFile);
      } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
      }
      return NULL;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM