Pierre-Luc Bolduc
Pierre-Luc Bolduc

Reputation: 495

Upload a big PHP file to Google Drive

I am able to upload a big file from my server to Google drive, but when I'm uploading a file from my server, I can't access my website as long as the process is not done. Do you know what I can do about that?

There is my script for my upload:

set_time_limit(0);                    
$file = new Google_Service_Drive_DriveFile();
$file->title = "title";


$chunkSizeBytes = 1 * 1024 * 1024;

// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->insert($file);

// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
  $client,
  $request,
  mime_content_type($filePath),
  null,
  true,
  $chunkSizeBytes
);
$media->setFileSize(exec('stat -c %s "'.$filePath.'"'));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($filePath, "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
 }

// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
  $result = $status;                          

}

Upvotes: 1

Views: 294

Answers (2)

pinoyyid
pinoyyid

Reputation: 22286

if I understand you correctly, your problem is that your upload to Drive is blocking and until it completes you don't send a response to the browser. If that's the case, you need to delegate the Drive upload to a separate thread and let the primary thread send an http response and close.

Upvotes: 0

It sounds like what might be happening is that your connection to Google Drive might be hogging the bandwidth. The chances are that even if you are on a dedicated server that your internal network connection might be limited by design (many have a 10Mb connection) and often further by firewall restrictions.

Although it is possible that your server itself is simply maxed out sending the file it is not that likely.

As to what you can do about it that could be a tough nut to crack. At the PHP level I am not aware of sufficiently low level control over socket connections in terms of packet rate limits and so forth.

If you are able to implement a bandwidth restricted connection it could solve things for you but failing that there is always "Plan B for intensive operations" which is queue such actions for late at night when it is massively unlikely that anyone is actually going to want to access the site and if they are then you are disrupting the least amount of people.

An alternative is to use some sort of cloud DNS system with caching such as cloudflare which would keep your content being served up to people even while the server drops out for a moment to do the big job.

Combine the two together and although the server stops responding during the upload it is likely to go unnoticed. It is far from the perfect solution but in terms of designing around something that might not be solvable it would be a solid working answer.

Upvotes: 1

Related Questions