progfa
progfa

Reputation: 311

PHP- Count files on a server that connected with FTP

How to count number of files on a remote server that connected with FTP?

This is my code but it doesn't work

<?php
    @$ftp = ftp_connect("host");
    @ftp_login($ftp, "usr", "pwd");
    ftp_chdir($ftp,'uploads/');
    echo count(glob(ftp_pwd($ftp) . '*'));
?>

Thanks!

Upvotes: 1

Views: 2630

Answers (3)

Try something like this

<?php
    @$ftp = ftp_connect("host");
    @ftp_login($ftp, "usr", "pwd");
    //ftp_chdir($ftp,'uploads/');
    //echo count(glob(ftp_pwd($ftp) . '*'));
    if ($handle = opendir(ftp_chdir($ftp,'uploads/'))) {
        while (($file = readdir($handle)) !== false){
            if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                $i++;
        }
    }
   echo "Total number of files:$i";

Upvotes: 0

BlitZ
BlitZ

Reputation: 12168

Try to use count() and ftp_nlist() functions combination:

$ftp = ftp_connect("host");
ftp_login($ftp, "usr", "pwd");
echo count(ftp_nlist($ftp, 'uploads/'));
ftp_close($ftp);

Upvotes: 4

davidkonrad
davidkonrad

Reputation: 85588

use ftp_rawlist :

$files = ftp_rawlist($ftp, '/');

echo count($files).' files ..';

instead of

echo count(glob(ftp_pwd($ftp) . '*'));

Upvotes: 2

Related Questions