1
votes

I'm using Google Drive api to upload files from my web site (php) to my Drive account. Since users are going to upload large files, I'm using "Resumable" upload type.

Here is the code from Google documantation:

function insertFile($service,$client,$filetoUpload) {
$file = new Google_Service_Drive_DriveFile();
$file->title = $filetoUpload['name'];
$chunkSizeBytes =   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,
  $filetoUpload['type'],
  null,
  true,
  $chunkSizeBytes
);
$media->setFileSize(filesize($filetoUpload['tmp_name']));

// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($filetoUpload['tmp_name'], "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;
  print_r($result);
}

fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
}

I tried set_time_limit(300); and it works for files around 50MB, but not for larger files.

Does my php have to execute for the whole upload process? Why it is not uploading each chunk separately so it uploads 1MB each time?

1
are you running this In the background say as a cron job or something? Or are you running it though a webpage via apache? - DaImTo
it is running through a webpage, it is actually a form (later handled by ajax to show the progress & stuff) where user selects their file and uploads it to my drive. - Ramtin Gh
I seam to remember there being a setting in the webserver that sets max time a script can run. 50mb seams to be a very specific number check the web server settings. - DaImTo
Yes, I can edit that from php.ini or set_time_limit from my script, but I'm asking if its possible to send file in chunks so it doesnt take too long, even if I increase that to 10 minutes, people might face a problem while uploading a 1GB file. - Ramtin Gh
The code you posted already uploads it in chunks "// Upload the various chunks. " :) - DaImTo

1 Answers

0
votes

Does my PHP have to execute for the whole upload process?

Yes for a PHP script to run it has to be executing. If you want the file uploaded then the script will need to run for the amount of time it takes for the file to upload.

Why it is not uploading each chunk separately so it uploads 1MB each time?

It should be you set the chunk size to around 1mb. Why not set a debug message in so you can see how many chunks it uploads.

$chunkSizeBytes =   1024 * 1024;

By setting set_time_limit(0); the script will run until it has completely upload your file. However you may still have issues with your webserver which could timeout the script after 5 minutes.