1

I want to download file from my Google Drive to my Server using php after selecting file from google picker, but Using the following code i cannot get it in the correct format (text format as shown below).It is getting saved in $upload_path='sss' folder. I am new to Google drive. Please help to sort out my problem.

   // A simple callback implementation.
    function pickerCallback(data) {
      if (data.action == google.picker.Action.PICKED) {
        var id = data.docs[0].id;
        var request = new XMLHttpRequest();
        request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
        request.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
        request.addEventListener('load', function() {
            var item = JSON.parse(request.responseText);
            console.log(item);
            downloadFile(item);
        });
        request.send();
      }
    }


function downloadFile(item) {
    var request = new XMLHttpRequest();
    var mimeType = item['mimeType'];
    if (typeof item.exportLinks != 'undefined') {
        if (mimeType == 'application/vnd.google-apps.spreadsheet') {
            mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        }
        url = item['exportLinks'][mimeType];
        link = url;
    } else {
        lien = item['downloadUrl'].split("&");
        link = lien[0] + '&' + lien[1];
        url = item['downloadUrl'];
    }
    title = item['title'];
    type = mimeType;
    filesize = item['fileSize'];
    fileext = item['fileExtension'];
    id = item['id'];
    var datatable = [url, title, type, filesize, fileext,id];

    request.open("POST", "ajax-db-files/copy_drive_file.php?" + datatable, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send("datatable=" + datatable);
}

Php file:

$upload_path='sss';
     if (isset($_POST['exportFormat'])) {
        $pieces = explode(",", $_POST['exportFormat']);
        $url = $_POST['datatable'] . '&exportFormat=xlsx';
        $title = $pieces[1];
        $type = $pieces[2];
        $fileext = $pieces[0];
        $fileId = $pieces[5];
    }else {
        $url = $_POST['datatable'] . '&e=download';
        $pieces = explode(",", $_POST['gd']);
        $onlytitle = explode(".", $pieces[1]);
        $title = $onlytitle[0];
        $type = $pieces[2];
        $filesize = $pieces[3];
        $fileext = $pieces[4];
        $fileId = $pieces[5];

    }
    $fullPath = $upload_path.'/'.$title.'.'. $fileext;
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: " . $type . "");
    header("Content-Disposition: attachment; filename=\"".$title.'.'.$fileext."\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $filesize);
    // folder to save downloaded files to. must end with slash
    $destination_folder = $upload_path.'/';
    $newfname = $destination_folder . basename($title . '.' . $fileext);
    $file = fopen($url, "rb");
    if ($file) {
        $newf = fopen($newfname, "wb");
        if ($newf)
            while (!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
            }
    }
    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
    ob_end_flush();

enter image description here

user3653474
  • 3,393
  • 6
  • 49
  • 135
  • Rivero that code is not working i have checked. Can u please correct the above code because i don't have much knowledge about the above topic. – user3653474 Sep 16 '15 at 18:14
  • Every time i select a file a file is uploaded with the content as shown in the image, what is this image content for i don't understand. – user3653474 Sep 16 '15 at 18:15
  • Please don't ask 5 separate questions on how to accomplish the same task – Claies Sep 17 '15 at 06:09

0 Answers0