简体   繁体   English

将大型PHP文件上传到Google云端硬盘

[英]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. 我可以将服务器上的大文件上传到Google驱动器,但是,当我从服务器上载文件时,只要未完成该过程,就无法访​​问我的网站。 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;                          

}

It sounds like what might be happening is that your connection to Google Drive might be hogging the bandwidth. 听起来好像正在发生的事情是您与Google云端硬盘的连接占用了带宽。 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. 即使您在专用服务器上,内部网络连接也有可能受到设计的限制(许多连接有10Mb的连接),并且常常受到防火墙的限制。

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. 在PHP级别上,我不了解在套接字速率方面对分组连接的控制是否足够低。

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. 如果您能够实现带宽受限的连接,则可以为您解决问题,但如果始终没有“密集运行计划B”,该计划会将此类操作排在深夜,而实际上根本没有人想要访问该网站,如果他们访问了,那么您正在破坏的人数最少。

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. 一种替代方法是使用某种带有缓存的云DNS系统,例如cloudflare,即使服务器暂时中断以完成重要工作,它也可以将您的内容提供给人们。

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. 这远非完美的解决方案,但就无法解决的问题进行设计而言,这将是一个切实可行的答案。

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. 在这种情况下,您需要将云端硬盘上传委派给单独的线程,并让主线程发送http响应并关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM