How to track progress and resume interrupted uploads with Google Drive API in PHP

I’m working with Google Drive API using PHP Client Library and need help with resumable uploads. I can already upload files in chunks but I want to track upload progress and handle interruptions properly.

function processFileUpload($driveService, $apiClient, $targetFile, $folderId) {
    $driveFile = new Google_Service_Drive_DriveFile();
    $driveFile->name = $targetFile['filename'];
    $pieceSize = 2 * 1024 * 1024;
    
    if ($folderId != null) {
        $folder = new Google_Service_Drive_ParentReference();
        $folder->setId($folderId);
        $driveFile->setParents(array($folder));
    }
    
    $apiClient->setDefer(true);
    $uploadRequest = $driveService->files->insert($driveFile);
    
    $mediaUploader = new Google_Http_MediaFileUpload(
        $apiClient,
        $uploadRequest,
        $targetFile['mimetype'],
        null,
        true,
        $pieceSize
    );
    $mediaUploader->setFileSize(filesize($targetFile['path']));
    
    $uploadComplete = false;
    $fileHandle = fopen($targetFile['path'], "rb");
    
    while (!$uploadComplete && !feof($fileHandle)) {
        set_time_limit(180);
        $dataPiece = fread($fileHandle, $pieceSize);
        $uploadComplete = $mediaUploader->nextChunk($dataPiece);
    }
    
    fclose($fileHandle);
    $apiClient->setDefer(false);
    
    return $uploadComplete;
}

The problem is I need to check upload status and resume failed uploads. How can I capture the session URI and make custom requests to check progress? Do I need a separate script for handling these status checks?

For resumable uploads, you need to save the upload session data. After you initialize MediaFileUpload, grab the session URI with getResumeUri() and store it with the byte count in your database. When resuming, rebuild the MediaFileUpload object using that saved URI and pick up where you left off. Track progress with a callback that calculates percentage by dividing ftell($fileHandle) by total file size. Set up a JSON endpoint that your frontend can poll for progress updates. Handle HTTP codes properly: 308 means keep uploading, 200/201 means you’re done.

you can get the session URI from mediaUploader by using getResumeUri(). save it in db or session to resume if upload fails. for tracking, divide bytes uploaded by total size and show percentage. easy peasy!

Your code’s missing session persistence and error handling. Set up a database table for upload sessions - include session_uri, file_path, bytes_uploaded, and status fields. Save the session data with $mediaUploader->getResumeUri() before you start the upload loop. Then wrap your while loop in try-catch blocks and save progress after each chunk succeeds. When uploads fail, send a PUT request to the stored session URI to get the current byte range and pick up where you left off. I run a separate cron job that finds stale incomplete uploads and auto-resumes them. For progress tracking, just divide the current file pointer position by total file size and update your database record - makes it easy for your frontend to show real-time progress.