new_b
new_b

Reputation: 63

Separate file & directories in PHP

PHP code used for listing the files and directories.

$buff = ftp_nlist($conn_id,$dir );

$buff is an array of both files & directories.

Is there a way to get 2 seperate arrays , 1 contains just files and other just directories.

Upvotes: 2

Views: 2778

Answers (4)

rc_luke
rc_luke

Reputation: 111

It is possible to pattern match the files you need. If you are able to rely on files having extensions and directories without a "." in, you can get all the filenames rather simply like so:

$filenames = ftp_nlist( $conn_id, '*.*' );

Upvotes: 1

Salman Arshad
Salman Arshad

Reputation: 272096

Did you try ftp_rawlist()? This function returns an array containing rows like:

06-21-11  01:49PM       <DIR>          01
06-21-11  02:20PM       <DIR>          02
06-21-11  03:33AM                  456 default.html
06-21-11  04:59PM                  123 robots.txt

Or:

drwxr-xr-x   12 ftp      ftp          4096 Mar 25  1999 ACS
drwxr-xr-x   14 ftp      ftp          4096 Mar 18  2001 ATHENA
drwxr-xr-x    2 ftp      ftp          4096 Jul 10  1998 XNeXT
drwxr-xr-x    3 ftp      ftp          4096 Jul 10  1998 elib
drwxrwxr-x  349 ftp      ftp         98304 Jul 02 06:30 gnu
drwxr-xr-x    3 ftp      ftp          4096 Jul 10  1998 lpf
-rw-r--r--    1 ftp      ftp       3448992 Oct 16  2003 ls-lR
drwxr-xr-x    2 ftp      ftp          4096 Jul 10  1998 palladium
drwxr-xr-x    2 ftp      ftp          4096 Jul 10  1998 palladium2
drwxr-xr-x    2 ftp      ftp          4096 Jul 10  1998 virus

From this point forward you can use ftp_systype() function to determine how to interpret the above responses.

Or use heuristics e.g. on Windows the directories will contain the string <DIR> where as on Linux you should find each directory beginning with d followed by something like rwxrwxrwx.

Upvotes: 2

Kazuo
Kazuo

Reputation: 397

You could loop through the array and check wheter the item is a directory by using a function like

function ftp_is_dir($conn_id, $dir) {
  if (ftp_chdir($conn_id, $dir)) {
    ftp_chdir($conn_id, '..');
    return true;
  } else {
    return false;
  }
}

EDIT:

Another possibility would be

<?php
function is_ftp_dir($file, $conn_id){
if(ftp_size($conn_id, $file) == '-1'){
    return true;
}else{
    return false;
}
}
?>

Please note that not every ftp server supports ftp_size().

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

From the documentation:

directory

The directory to be listed. This parameter can also include arguments, eg. ftp_nlist($conn_id, "-la /your/dir"); Note that this parameter isn't escaped so there may be some issues with filenames containing spaces and other characters.

So perform ftp_nlist twice: once providing arguments that give you only files, and then providing arguments that give you only directories. This will rely on the system where the FTP server sites.

Alternatively, you can employ this more verbose solution that uses ftp_chdir, or this one that uses ftp_size.

Upvotes: 3

Related Questions