How to Get File Size of Files on FTP Server?

2019-09-06 14:41发布

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条回答
劳资没心,怎么记你
2楼-- · 2019-09-06 14:55

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-09-06 14:59

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?

查看更多
Luminary・发光体
4楼-- · 2019-09-06 15:03

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

查看更多
劫难
5楼-- · 2019-09-06 15:08

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

查看更多
男人必须洒脱
6楼-- · 2019-09-06 15:12

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.

查看更多
冷血范
7楼-- · 2019-09-06 15:17

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

查看更多
登录 后发表回答