使用 PHP 或 Python 从 FTP 服务器读取或下载 5kb 文件,而不是下载或读取整个文件 [英] Read or download 5kb of a file from FTP server in PHP or Python instead of downloading or reading whole file

查看:17
本文介绍了使用 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 文件,而不是下载或读取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆