• <tfoot id='vnR9a'></tfoot>

    <small id='vnR9a'></small><noframes id='vnR9a'>

    1. <legend id='vnR9a'><style id='vnR9a'><dir id='vnR9a'><q id='vnR9a'></q></dir></style></legend>
        <bdo id='vnR9a'></bdo><ul id='vnR9a'></ul>

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

        不使用中间文件将内存数据传输到 FTP 服务器

        时间:2023-10-31
        <tfoot id='KaWIl'></tfoot>
            <i id='KaWIl'><tr id='KaWIl'><dt id='KaWIl'><q id='KaWIl'><span id='KaWIl'><b id='KaWIl'><form id='KaWIl'><ins id='KaWIl'></ins><ul id='KaWIl'></ul><sub id='KaWIl'></sub></form><legend id='KaWIl'></legend><bdo id='KaWIl'><pre id='KaWIl'><center id='KaWIl'></center></pre></bdo></b><th id='KaWIl'></th></span></q></dt></tr></i><div id='KaWIl'><tfoot id='KaWIl'></tfoot><dl id='KaWIl'><fieldset id='KaWIl'></fieldset></dl></div>
              <tbody id='KaWIl'></tbody>
              <bdo id='KaWIl'></bdo><ul id='KaWIl'></ul>

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

                  <legend id='KaWIl'><style id='KaWIl'><dir id='KaWIl'><q id='KaWIl'></q></dir></style></legend>

                2. 本文介绍了不使用中间文件将内存数据传输到 FTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些我用 PHP 的 json_encode() 编码的 JSON 数据,它看起来像这样:

                  I am having some JSON data that I encoded it with PHP's json_encode(), it looks like this:

                  {
                      "site": "site1",
                      "nbrSicEnt": 85,
                  }
                  

                  我想做的是将数据直接作为文件写入FTP服务器.

                  What I want to do is to write the data directly as a file onto an FTP server.

                  出于安全原因,我不希望在将文件发送到 FTP 服务器之前先在本地创建文件,我希望它是动态创建的.因此,例如不使用 tmpfile().

                  For security reasons, I don't want the file to be created locally first before sending it to the FTP server, I want it to be created on the fly. So without using tmpfile() for example.

                  当我阅读 ftp_put 的 php 文档时:

                  When I read the php documentations for ftp_put:

                  bool ftp_put ( resource $ftp_stream , string $remote_file , 
                                 string $local_file , int $mode [, int $startpos = 0 ] )
                  

                  在写入远程文件之前,需要创建一个本地文件(string $local_file).

                  Ones needs to create a local file (string $local_file) before writing it to the remote file.

                  我正在寻找一种直接写入 remote_file 的方法.如何使用 PHP 做到这一点?

                  I am looking for a way to directly write into the remote_file. How can I do that using PHP?

                  推荐答案

                  file_put_contents 是最简单的解决方案:

                  The file_put_contents is the easiest solution:

                  file_put_contents('ftp://username:password@hostname/path/to/file', $contents);
                  

                  如果它不起作用,可能是因为您没有启用 URL 包装器PHP.

                  If it does not work, it's probably because you do not have URL wrappers enabled in PHP.

                  如果您需要更好地控制写入(传输模式、被动模式、偏移量、读取限制等),请使用 ftp_fput 带有 php://temp(或 php://memory)流:

                  If you need greater control over the writing (transfer mode, passive mode, offset, reading limit, etc), use the ftp_fput with a handle to the php://temp (or the php://memory) stream:

                  $conn_id = ftp_connect('hostname');
                  
                  ftp_login($conn_id, 'username', 'password');
                  ftp_pasv($conn_id, true);
                  
                  $h = fopen('php://temp', 'r+');
                  fwrite($h, $contents);
                  rewind($h);
                  
                  ftp_fput($conn_id, '/path/to/file', $h, FTP_BINARY, 0);
                  
                  fclose($h);
                  ftp_close($conn_id);
                  

                  (添加错误处理)

                  或者您可以直接在 FTP 服务器上打开/创建文件.如果文件很大,这特别有用,因为您不会将全部内容保存在内存中.

                  Or you can open/create the file directly on the FTP server. That's particularly useful, if the file is large, as you won't have keep whole contents in memory.

                  请参阅使用 PHP 在外部 FTP 服务器上生成 CSV 文件.

                  这篇关于不使用中间文件将内存数据传输到 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:来自 PHP 内部的 SFTP 下一篇:列出并从 FTP 下载点击的文件

                  相关文章

                3. <legend id='84TrJ'><style id='84TrJ'><dir id='84TrJ'><q id='84TrJ'></q></dir></style></legend>

                        <bdo id='84TrJ'></bdo><ul id='84TrJ'></ul>
                      <i id='84TrJ'><tr id='84TrJ'><dt id='84TrJ'><q id='84TrJ'><span id='84TrJ'><b id='84TrJ'><form id='84TrJ'><ins id='84TrJ'></ins><ul id='84TrJ'></ul><sub id='84TrJ'></sub></form><legend id='84TrJ'></legend><bdo id='84TrJ'><pre id='84TrJ'><center id='84TrJ'></center></pre></bdo></b><th id='84TrJ'></th></span></q></dt></tr></i><div id='84TrJ'><tfoot id='84TrJ'></tfoot><dl id='84TrJ'><fieldset id='84TrJ'></fieldset></dl></div>

                    1. <tfoot id='84TrJ'></tfoot>
                    2. <small id='84TrJ'></small><noframes id='84TrJ'>