简体   繁体   English

如何获取FTP服务器上文件的文件大小?

[英]How to Get File Size of Files on FTP Server?

I need a list of all the files (and associated file sizes) on an FTP server. 我需要一个FTP服务器上所有文件(以及相关文件大小)的列表。 I can get a list of files using CodeIgniter's FTP class, but no idea how to get the file size. 我可以使用CodeIgniter的FTP类获取文件列表,但不知道如何获取文件大小。 How do I get the file sizes? 如何获取文件大小? Thanks. 谢谢。

Just had a quick look at CodeIgniters FTP class.. As its written with backwards compat for PHP4 you could probabaly do this (hack job) if you must. 只需快速浏览一下CodeIgniters FTP类即可。正如它是为PHP4向后兼容编写的,如果需要的话,您可能会做到这一点(破解工作)。

<?php
$files = $this->ftp->list_files('/folder/');

foreach ($files as $file)
{
  echo 'File:'. $file .' Size: '. ftp_size($this->ftp->conn_id, $file) .' <br />';
}
$this->ftp->close();

I wouldn't recommend this - It's probabably worth extending the main CI ftp class 我不推荐这样做-可能值得扩展主要的CI ftp类

class FTP extends CI_FTP
{

  function FTP()
  {
    // call parent constructor
    parent::CI_FTP();
  }

  // Single file size
  function file_size($file)
  {
    return ftp_size($this->conn_id, $file);
  }
}

Put the above into your application/libraries and save it as ftp.php. 将以上内容放入您的应用程序/库,并将其另存为ftp.php。 If you're running an updated version of CI it'll load your extension. 如果您正在运行CI的更新版本,则会加载您的扩展程序。

I know nothing about CodeIgniter's FTP class, but what about this? 我对CodeIgniter的FTP类一无所知,但是那又如何呢?

http://www.php.net/manual/en/function.ftp-rawlist.php http://www.php.net/manual/en/function.ftp-rawlist.php

I assume that the FTP class' list_files() method doesn't provide this info. 我假设FTP类的list_files()方法不提供此信息。 Is that correct? 那是对的吗?

After hard work, this code works great!!!! 经过艰苦的努力,这段代码很棒!!!! and I want to share with the community (by MundialSYS) 我想与社区分享(通过MundialSYS)

function dirFTPSize($ftpStream, $dir) {
    $size = 0;
    $files = ftp_nlist($ftpStream, $dir);

    foreach ($files as $remoteFile) {
        if(preg_match('/.*\/\.\.$/', $remoteFile) || preg_match('/.*\/\.$/', $remoteFile)){
            continue;
        }
        $sizeTemp = ftp_size($ftpStream, $remoteFile);
        if ($sizeTemp > 0) {
            $size += $sizeTemp;
        }elseif($sizeTemp == -1){//directorio
            $size += dirFTPSize($ftpStream, $remoteFile);
        }
    }

    return $size;
}

$hostname = '127.0.0.1'; // or 'ftp.domain.com'
$username = 'username';
$password = 'password';
$startdir = '/public_html'; // absolute path
$files = array();
$ftpStream = ftp_connect($hostname);
$login = ftp_login($ftpStream, $username, $password);
if (!$ftpStream) {
    echo 'Wrong server!';
    exit;
} else if (!$login) {
    echo 'Wrong username/password!';
    exit;
} else {
    $size = dirFTPSize($ftpStream, $startdir);
}

echo number_format(($size / 1024 / 1024), 2, '.', '') . ' MB';

ftp_close($ftpStream);

Good code! 好代码! Fernando 费尔南多

Its relatively simple to extend the CI FTP class: 扩展CI FTP类相对简单:

class MY_FTP extends CI_FTP {

    function MY_FTP()
    {
        parent::CI_FTP();
    }

    function get_file_size()
    {

    }
}

Essentially just make get_ftp_size() a wrapper for: 本质上,只需将get_ftp_size()包装为:

return ftp_size($conn, $file);

http://php.net/manual/en/function.ftp-size.php http://php.net/manual/en/function.ftp-size.php

I hope this helps (if you are stuck just skim through the ftp.php file of your install; you should soon find your way) 希望对您有所帮助(如果您只是在安装过程中浏览ftp.php文件而被卡住;您应该尽快找到路)

Edit 编辑

As wimvds rightly suggest's ftp_rawlist() might be the more preferable/easier option, i may even go so far as to suggest altering list_files() to use ftp_rawlist(). 正如wimvds正确地建议使用ftp_rawlist()可能是更可取/更简便的选择,我什至甚至建议更改list_files()以使用ftp_rawlist()。

Note that there is a chanche that the FTP server has ceratin functions disabled, or it won't let you call then(for example the filesize() function). 请注意,有一种说法是FTP服务器禁用了ceratin功能,否则将不允许您调用该功能(例如filesize()函数)。 Also, the standard filesize() http://php.net/manual/en/function.filesize.php functionshould work over ftp 另外,标准的filesize() http://php.net/manual/en/function.filesize.php函数应该可以在ftp上工作

Since CI is still PHP 4 compatible you probably can do it quick and dirty as follows : 由于CI仍然与PHP 4兼容,因此您可以按照以下步骤进行操作:

$this->load->library('ftp');

$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;

$this->ftp->connect($config);
$files = ftp_rawlist($this->ftp->conn_id, $path);

In $files you should get one line per file, containing file permissions, owner/group, filesize and filename. 在$ files中,每个文件应该包含一行,其中包含文件权限,所有者/组,文件大小和文件名。 You will have to parse this to display them separately. 您将必须对此进行解析以单独显示它们。

DISCLAIMER : Just copy/pasted the connection info from the CI manual, added the last line based on the CI source code, YMMV :p. 免责声明:只需从CI手册中复制/粘贴连接信息,并基于CI源代码YMMV:p添加最后一行。

Using the file helper 使用文件助手

get_file_info('path/to/file', $file_information)

Given a file and path, returns the name, path, size, date modified. 给定文件和路径,返回名称,路径,大小,修改日期。 Second parameter allows you to explicitly declare what information you want returned; 第二个参数允许您显式声明要返回的信息; options are: name, server_path, size, date, readable, writable, executable, fileperms. 选项包括:名称,server_path,大小,日期,可读,可写,可执行文件,文件权限。 Returns FALSE if the file cannot be found. 如果找不到文件,则返回FALSE。

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

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