How to upload block by block to Google Drive to avoid using high memory?

Hello,

How to upload block by block to Google Drive to avoid using high memory?

I saw such an explanation somewhere, but it didn’t give an example of how to do it yet.

Explanation of large memory usage: You read the file using the following line:

$data = file_get_contents(‘myfile.zip’);

This requires PHP to read the full file contents into memory ( in var $data ). It would be better to read the file blockwise e.g. in 4096 byte blocks and send them to network immediately.

Memory error occurs when uploading a 362 MB file.
I am using the code below
How should I change it to load it into the block mentioned in the above description?

$fileMetadata = new Google_Service_Drive_DriveFile();
$fileMetadata->setName($file);
$fileMetadata->setParents([$createdFolder->id]);
$createdFile = $service->files->create($fileMetadata, [
	'data' => file_get_contents($filePath),
	'mimeType' => mime_content_type($filePath),
	'uploadType' => 'media',
]);

I was able to upload large files very quickly using the code in the url below.

I want to check whether the uploaded file exists and if so, overwrite it.

Error message;

Fatal error: Uncaught Error: Call to undefined method GuzzleHttp\Psr7\Request::getFiles() in

The code given by the error;

if (count($results->getFiles()) > 0) {

Library;

$client = new Google\Client();
$client->setAuthConfig('**************.json');
$client->addScope(Google\Service\Drive::DRIVE);
$service = new Google\Service\Drive($client);

However, I get an error from the function that checks whether the file exists or not. What is the error here?

<?php 

    function searchFile($service, $parentId, $fileName) {

        $results = $service->files->listFiles([
            'q' => "name='$fileName' and '$parentId' in parents",
        ]);

        if (count($results->getFiles()) > 0) {
            return $results->getFiles()[0];
        } else {
            return null;
        }
    }





    $existingFile = searchFile($service, $createdFolder->id, $dosya);

    if ($existingFile) {

        $file = new Google\Service\Drive\DriveFile();
        $file->name = $dosya;
        $chunkSizeBytes = 1 * 1024 * 1024;

        $client->setDefer(true);

        $media = new Google\Http\MediaFileUpload(
            $client,
            $service->files->update($existingFile->getId(), $file),
            'text/plain',
            null,
            true,
            $chunkSizeBytes
        );

        $media->setFileSize(filesize($filePath));

        function readVideoChunk($handle, $chunkSize)
        {
            $byteCount = 0;
            $giantChunk = "";
            while (!feof($handle)) {
                $chunk = fread($handle, 8192);
                $byteCount += strlen($chunk);
                $giantChunk .= $chunk;
                if ($byteCount >= $chunkSize) {
                    return $giantChunk;
                }
            }
            return $giantChunk;
        }

        $status = false;
        $handle = fopen($filePath, "rb");
        while (!$status && !feof($handle)) {
            $chunk = readVideoChunk($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }

        $result = $status;
        fclose($handle);


    }else{

    }

Thank you from now

Sponsor our Newsletter | Privacy Policy | Terms of Service