• <small id='vDLJd'></small><noframes id='vDLJd'>

    • <bdo id='vDLJd'></bdo><ul id='vDLJd'></ul>

    <legend id='vDLJd'><style id='vDLJd'><dir id='vDLJd'><q id='vDLJd'></q></dir></style></legend>
      1. <tfoot id='vDLJd'></tfoot>
        <i id='vDLJd'><tr id='vDLJd'><dt id='vDLJd'><q id='vDLJd'><span id='vDLJd'><b id='vDLJd'><form id='vDLJd'><ins id='vDLJd'></ins><ul id='vDLJd'></ul><sub id='vDLJd'></sub></form><legend id='vDLJd'></legend><bdo id='vDLJd'><pre id='vDLJd'><center id='vDLJd'></center></pre></bdo></b><th id='vDLJd'></th></span></q></dt></tr></i><div id='vDLJd'><tfoot id='vDLJd'></tfoot><dl id='vDLJd'><fieldset id='vDLJd'></fieldset></dl></div>

        如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能

        How to increase the performance of a file upload using native sftp functions and fwrite in PHP(如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能)
        • <legend id='HCgv4'><style id='HCgv4'><dir id='HCgv4'><q id='HCgv4'></q></dir></style></legend><tfoot id='HCgv4'></tfoot>
              <tbody id='HCgv4'></tbody>
                <bdo id='HCgv4'></bdo><ul id='HCgv4'></ul>
                <i id='HCgv4'><tr id='HCgv4'><dt id='HCgv4'><q id='HCgv4'><span id='HCgv4'><b id='HCgv4'><form id='HCgv4'><ins id='HCgv4'></ins><ul id='HCgv4'></ul><sub id='HCgv4'></sub></form><legend id='HCgv4'></legend><bdo id='HCgv4'><pre id='HCgv4'><center id='HCgv4'></center></pre></bdo></b><th id='HCgv4'></th></span></q></dt></tr></i><div id='HCgv4'><tfoot id='HCgv4'></tfoot><dl id='HCgv4'><fieldset id='HCgv4'></fieldset></dl></div>

                1. <small id='HCgv4'></small><noframes id='HCgv4'>

                2. 本文介绍了如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我正在使用以下代码将一个大文件 (500MB) 上传到 sftp 服务器.

                  Hi I am using the following code to upload a huge file (500MB) to a sftp server.

                  <?php
                  
                  $connection = ssh2_connect($this->host, $this->port, null);
                  $sftp = ssh2_sftp($connection);
                  
                  $connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
                  $stream = fopen('ssh2.sftp://' . $connection_string, 'w');
                  $source = fopen($localFilepath, 'r');
                  
                  if (!$stream) {
                      throw new Exception('Could not create file: ' . $connection_string);
                  }
                  
                  while (!feof($source)) {
                      // Chunk size 32 MB
                      if (fwrite($stream, fread($source, 33554432)) === false) {
                          throw new Exception('Could not send data: ' . $connection_string);
                      }
                  }
                  
                  fclose($source);
                  fclose($stream);
                  

                  但上传速度非常慢.代码在 Google Cloud Run 上运行.上传速度约为 8 MiB/s.

                  But the upload is very slow. The code is running on Google Cloud Run. The upload speed is around 8 MiB/s.

                  我也尝试通过 shell_exec 使用 lftp,但由于 Cloud Run,这会导致更多问题.

                  I also tried to use lftp via shell_exec but this lead to even more issues due to Cloud Run.

                  上行链路不是问题,因为我可以通过 CURL post 发送文件而没有任何问题.

                  The uplink can't be the problem as I can send files via CURL post without any issues.

                  有人可以帮忙吗?

                  非常感谢和最好的,intxcc

                  Many thanks and best, intxcc

                  推荐答案

                  问题是即使 32MB 被读取然后写入 sftp 流,fwrite 也会以不同的大小分块.我想只有几 KB.

                  The issue is that even though 32MB are read and then written to the sftp stream, fwrite will chunk at a different size. I think just a few KB.

                  对于文件系统(这是 fwrite 的常见情况),这很好,但由于 fwrite 到远程服务器而导致高延迟.

                  For filesystems (which is the usual case with fwrite) this is fine, but not with high latency due to fwriting to a remote server.

                  所以解决方法是增加sftp流的chunk大小

                  So the solution is to increase the chunk size of the sftp stream with

                  stream_set_chunk_size($stream, 1024 * 1024);
                  

                  所以最终的工作代码是:

                  So the final working code is:

                  <?php
                  
                  $connection = ssh2_connect($this->host, $this->port, null);
                  $sftp = ssh2_sftp($connection);
                  
                  $connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
                  $stream = fopen('ssh2.sftp://' . $connection_string, 'w');
                  $source = fopen($localFilepath, 'r');
                  
                  // Stream chunk size 1 MB
                  stream_set_chunk_size($stream, 1024 * 1024);
                  
                  if (!$stream) {
                      throw new Exception('Could not create file: ' . $connection_string);
                  }
                  
                  while (!feof($source)) {
                      // Chunk size 32 MB
                      if (fwrite($stream, fread($source, 33554432)) === false) {
                          throw new Exception('Could not send data: ' . $connection_string);
                      }
                  }
                  
                  fclose($source);
                  fclose($stream);
                  

                  希望这有助于下一个白发的人试图弄清楚这一点;)

                  Hope this helps the next person that is getting gray hair trying to figure that out ;)

                  这篇关于如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  PHP Upload File Validation(PHP 上传文件验证)
                  PHP Error - Uploading a file(PHP 错误 - 上传文件)
                  How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                  php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                  How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                  change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
                  <tfoot id='2HXSI'></tfoot>

                    <i id='2HXSI'><tr id='2HXSI'><dt id='2HXSI'><q id='2HXSI'><span id='2HXSI'><b id='2HXSI'><form id='2HXSI'><ins id='2HXSI'></ins><ul id='2HXSI'></ul><sub id='2HXSI'></sub></form><legend id='2HXSI'></legend><bdo id='2HXSI'><pre id='2HXSI'><center id='2HXSI'></center></pre></bdo></b><th id='2HXSI'></th></span></q></dt></tr></i><div id='2HXSI'><tfoot id='2HXSI'></tfoot><dl id='2HXSI'><fieldset id='2HXSI'></fieldset></dl></div>
                      <legend id='2HXSI'><style id='2HXSI'><dir id='2HXSI'><q id='2HXSI'></q></dir></style></legend>
                        <tbody id='2HXSI'></tbody>

                        • <bdo id='2HXSI'></bdo><ul id='2HXSI'></ul>

                          <small id='2HXSI'></small><noframes id='2HXSI'>