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