dqcuq4138
dqcuq4138
2010-07-09 09:14

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

I need a list of all the files (and associated file sizes) on an FTP server. I can get a list of files using CodeIgniter’s FTP class, but no idea how to get the file size. How do I get the file sizes? Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • du3979 du3979 11年前

    I know nothing about CodeIgniter's FTP class, but what about this?

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

    I assume that the FTP class' list_files() method doesn't provide this info. Is that correct?

    点赞 评论 复制链接分享
  • doubi6303 doubi6303 11年前

    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. Returns FALSE if the file cannot be found.

    点赞 评论 复制链接分享
  • dqj96395 dqj96395 7年前

    After hard work, this code works great!!!! and I want to share with the community (by 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

    点赞 评论 复制链接分享
  • doutan3040 doutan3040 11年前

    Its relatively simple to extend the CI FTP class:

    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:

    return ftp_size($conn, $file);
    

    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)

    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().

    点赞 评论 复制链接分享
  • dr2898 dr2898 11年前

    Since CI is still PHP 4 compatible you probably can do it quick and dirty as follows :

    $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. 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.

    点赞 评论 复制链接分享
  • dsyq40772 dsyq40772 11年前

    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). Also, the standard filesize() http://php.net/manual/en/function.filesize.php functionshould work over ftp

    点赞 评论 复制链接分享
  • dongmimeng5500 dongmimeng5500 11年前

    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.

    <?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

    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. If you're running an updated version of CI it'll load your extension.

    点赞 评论 复制链接分享

为你推荐