Google驱动器apiphp执行时间错误

Google Drive api php execution time error

我正在使用 Google 云端硬盘 api 将文件从我的网站 (php) 上传到我的云端硬盘帐户。 由于用户要上传大文件,我使用 "Resumable" 上传类型。

这是 Google 文档中的代码:

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);
}

我试过 set_time_limit(300); 它适用于大约 50MB 的文件,但不适用于更大的文件。

我的 php 是否必须在整个上传过程中执行? 为什么它不单独上传每个块,而是每次上传 1MB?

我的 PHP 是否必须执行整个上传过程?

是的,PHP 到 运行 的脚本必须执行。如果您想要上传文件,那么脚本将需要 运行 文件上传所需的时间。

为什么不分块上传,每次上传1MB?

应该是你设置的chunk size在1mb左右。为什么不设置调试消息,这样您就可以看到它上传了多少块。

$chunkSizeBytes =   1024 * 1024;

通过设置set_time_limit(0);该脚本将 运行 直到它完全上传您的文件。但是,您的网络服务器可能仍然存在问题,可能会在 5 分钟后使脚本超时。