views:

68

answers:

6

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.

A: 

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.

stef
I don't see how this is supposed to work through an FTP connection?
Pekka
@pekka it doesnt, a misread i think.
DRL
+1  A: 

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?

George Marian
I think he wants to stick with CI's FTP functions (`ftp_rawlist()` would probably require at FTP connection opened using PHP's built-in functions). However, it looks like CI in fact doesn't offer the functionality.
Pekka
@Pekka: The CI FTP class is a simple wrapper class for the standard PHP FTP functions, so it's easy to extend it and add an extra wrapper function that executes ftp_rawlist. CI's list_files just calls ftp_nlist btw.
wimvds
@wim ah, makes sense.
Pekka
ftp_rawlist did it for me. Just had to parse for the parts I was interested in.Didn't want to use ftp_size as it's not always supported by servers.
StackOverflowNewbie
A: 

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

DRL
It will probably be easier to add a wrapper for [ftp_rawlist](http://www.php.net/manual/en/function.ftp-rawlist.php), so you can have both the filenames and the sizes in one FTP command... You will have to parse the result though (since it just returns one string per file).
wimvds
A: 

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

Quamis
A: 

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.

Kieran Allen
A: 

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.

wimvds