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

  2. <tfoot id='DqdYE'></tfoot>

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

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

      使用 PHP 或 Python 从 FTP 服务器读取或下载 5kb 文件,而不是下载或读取整个文件

      时间:2023-10-30
      • <bdo id='W8cwh'></bdo><ul id='W8cwh'></ul>

        <tfoot id='W8cwh'></tfoot>

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

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

                  <tbody id='W8cwh'></tbody>
                本文介绍了使用 PHP 或 Python 从 FTP 服务器读取或下载 5kb 文件,而不是下载或读取整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想从 FTP 服务器下载读取文件的一部分而不是下载整个文件,这是为了查看 FTP 服务器中存在的数据是否正确.

                I want to download or read part of a file from the FTP server instead of downloading the whole file, this is to just see the data that exists in the FTP server is correct.

                我们有这么多客户端,FTP 服务器中的每个文件都可能是任意大小,所以我不想下载阅读完整的文件,我只想下载或阅读文件的一部分,假设我只想要 5kb 的文件,或者如果从文件中获取第 100 行.

                We have so many clients and each file in the FTP server might be of any size, so instead of downloading or reading the complete file, I just want to download or read a part of the file, let's say I want only 5kb of file or if by line 100 lines from a file.

                我在 PHP 中有一个函数,如下所示,它完成了一半的工作,但对于较大的文件,它会失败.

                I have a function in PHP like below which does half of the work, but for larger files, it fails.

                function readByBytes($path)
                {
                    try
                    {
                        $handle = fopen($path, "rb");
                        if ($handle) 
                        {
                            while (($buffer = fgets($handle, 4096)) !== false)
                            {
                
                            }
                            if (!feof($handle))
                            {
                                echo "Error: unexpected fgets() fail
                ";
                            }
                            fclose($handle);
                        }
                    }
                    catch (Exception $e)
                    {
                        echo $e;
                    }
                }
                
                
                $filename = "ftp://username:password@127.0.0.1/prod/clientfeed.csv";
                
                $iterator = readByBytes($filename);
                
                foreach ($iterator as $key => $iteration)
                {
                    /// if file read is 5kb or some 100 lines
                    break;
                }
                

                有人可以用 PHP 或 Python 帮助我或指导我吗

                Can somebody help me or guide me on this in PHP or Python

                出现以下警告错误

                PHP Warning:  fopen(ftp://...@127.0.0.1/prod/clientfeed.csv): failed 
                to open stream: FTP server reports 550 Could not get file size.
                 in /var/www/html/newLpplugins/ftp_read_line_line.php on line 80
                PHP Warning:  filesize(): stat failed for 
                ftp://...@127.0.0.1/prod/clientfeed.csv in 
                /var/www/html/newLpplugins/ftp_read_line_line.php on line 81
                PHP Warning:  fread() expects parameter 1 to be resource, bool given 
                in /var/www/html/newLpplugins/ftp_read_line_line.php on line 81
                PHP Warning:  fclose() expects parameter 1 to be resource, bool given 
                in /var/www/html/newLpplugins/ftp_read_line_line.php on line 82
                PHP Warning:  
                file_get_contents(ftp://...@127.0.0.1/prod/clientfeed.csv): failed to 
                open stream: FTP server reports 550 Could not get file size.
                in /var/www/html/newLpplugins/ftp_read_line_line.php on line 84
                

                提前致谢.

                推荐答案

                如果你只想读取文件的一部分,那么只需删除你的 while 循环并调用 fgets 只有一次.

                If you want to read only part of the file, then just remove your while loop and call fgets only once.

                $buffer = fgets($handle, 4096);
                


                虽然如果文件是二进制文件或者你想读取固定数量的字节,你最好使用 fread.

                $buffer = fread($handle, 4096);
                


                虽然您的服务器与 PHP URL 包装器不兼容,但请参阅:
                获取FTP 服务器报告 550 无法获取文件大小."在 fopen 中使用 FTP URL 时
                而且 PHP 没有提供任何其他强大的替代方案来满足您的需求.


                Though your server is not compatible with PHP URL wrappers, see:
                Getting "FTP server reports 550 Could not get file size." when using FTP URL in fopen
                And PHP does not offer any other robust alternative for your needs.

                虽然在 Python 中使用 ftplib 是可行的:

                Though it is doable in Python with ftplib:

                ftp = FTP()
                ftp.connect(host, user, passwd)
                
                size = 4096
                
                cmd = "RETR {}".format(filename)
                f = BytesIO()
                aborted = False
                
                def gotdata(data):
                    f.write(data)
                    while (not aborted) and (f.tell() >= size):
                        ftp.abort()
                        aborted = True
                
                try:
                    ftp.retrbinary(cmd, gotdata)
                except:
                    # An exception when transfer is aborted is expected
                    if not aborted:
                        raise
                
                f.seek(0)
                

                代码基于我对以下问题的回答:
                在 FTP 服务器上的 zip 文件中获取文件名,而无需下载整个存档

                The code is based on my answer to:
                Get files names inside a zip file on FTP server without downloading whole archive

                这篇关于使用 PHP 或 Python 从 FTP 服务器读取或下载 5kb 文件,而不是下载或读取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何从命令行同步 FTP 目录? 下一篇:PHP:如何避免读取通过 FTP 推送给我的部分文件?

                相关文章

                  <small id='42vlT'></small><noframes id='42vlT'>

                    <legend id='42vlT'><style id='42vlT'><dir id='42vlT'><q id='42vlT'></q></dir></style></legend>

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