PHP code example of aslamhus / google-drive-uploader

1. Go to this page and download the library: Download aslamhus/google-drive-uploader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aslamhus / google-drive-uploader example snippets


putenv('GOOGLE_APPLICATION_CREDENTIALS=<path-to-service-account-credentials-json-file>');

use Aslamhus\GoogleDrive\GoogleDriveUploader;
$uploader = new GoogleDriveUploader($driveFolderId);
$file = $uploader->uploadBasic($filePath, $fileName, $mimeType);

$uploader = new GoogleDriveUploader($driveFolderId);
$resumeUri = $uploader->initResumable($fileName, $mimeType);
$uploader->startResumable($filePath);

$uploader = new GoogleDriveUploader($driveFolderId);
$resumeUri = $uploader->initResumable($fileName, $mimeType);
// the second argument to upload resumable is the optional onChunkRead callback
$asyncTask = $uploader->startResumable($filePath, null, true);

foreach ($asyncTask as $response) {
    // perform other logic
    // you can abort the upload at any time by calling $uploader->abort()
    // $response will return false until the upload is complete
}

// Once the upload is complete, $response will contain a Google Drive File object
$fileId = $response['id'];

$uploader = new GoogleDriveUploader($driveFolderId);
// init and store the resumable uri
$resumeUri = $uploader->initResumable($fileName, $mimeType);
$uploader->startResumable($filePath);
// If the upload is interrupted, resume it with the resume() method passing the resumeUri argument
$uploader->resume($resumeUri);

/**
 * On chunk read
 *
 * @param string $chunk - the chunk byte string
 * @param int $progress - the progress percentage, e.g. 10.00
 * @param int $fileSize - the file size in bytes
 * @param arary $byteRange - the range of bytes read [from, to], i.e. [0, 256000]
 **/
$onChunkRead = function($chunk,  $progress, $fileSize, $byteRange) {
    // do something like outputting the progress
    echo $progress . "%";
}
$uploader->startResumable($filePath, $onChunkRead);