4

I am working on a script for connecting to an FTP and then download the content.

Now my problem is to detect whether a each item is a folder or a file. My first idea was to try and use ftp_chdir, but this would require me to suppress the error using @, and I don't wish to do this (I don't wish to suppress errors, but instead prefer to handle them correctly).

Is there another way to check if a my items are files or folders?

3
  • Try explode() on every name of the file/folder taking . (dot) as a separator; if you are getting extension i.e. two array elements then it is file otherwise folder. Commented Apr 22, 2015 at 13:39
  • 1
    Ashwani: Sadly it is possible to have dots in folder names
    – Sinnbeck
    Commented Apr 22, 2015 at 13:46
  • 1
    user1844933: is_dir() does not seem to work for ftp functions
    – Sinnbeck
    Commented Apr 22, 2015 at 13:48

3 Answers 3

4

The bad thing about trying the ftp_chdir is not the need to suppress the errors. That's ok, as long as you have a legitimate reason to expect an error. It's rather the side affect of changing the directory.

If I take that direction, I'd try the ftp_size instead, as it does not have any side effects. It should fail for directories and succeed for files.


The ideal solution is to use the MLSD FTP command that returns a reliable machine-readable directory listing. But PHP supports that only since 7.2 with its ftp_mlsd function. Check the "type" entry for dir value.

Or, there's an implementation of the MLSD in user comments of the ftp_rawlist command:
https://www.php.net/manual/en/function.ftp-rawlist.php#101071

First check if your FTP server supports MLSD before taking this approach, as not all FTP servers do (particularly IIS and vsftpd don't).


Or, if you are connecting to one specific server, so you know its format of directory listing, you can use the ftp_rawlist, and parse its output to determine, if the entry is file or folder.

Typical listing on a *nix server is like:

drwxr-x---   3 vincent  vincent      4096 Jul 12 12:16 public_ftp
drwxr-x---  15 vincent  vincent      4096 Nov  3 21:31 public_html
-rwxrwxrwx   1 vincent  vincent        11 Jul 12 12:16 file.txt

It's the leading d that tells you, if the entry is a directory or not.


You may be lucky and in your specific case, you can tell a file from a directory by a file name (i.e. all your files have an extension, while subdirectories do not).

1
  • I have desided to use ftp_rawlist and check each line. Seems to work rather well. All the servers I am using are running Nix. Thanks!
    – Sinnbeck
    Commented Apr 24, 2015 at 6:08
0

In case someone need this in 2018+

You can use ftp_nlist. For files you'll retrieve array with only one element (the file path itself), for directories you'll have at least two items in array . and .., and if no file or directory exists there will be an empty array returned.

1
  • 1
    That's a behavior you indeed get with some FTP servers. But it's no way portable across various servers. And definitely it's not backed by FTP specification (the specification does not allow "listing" a file). Commented May 9, 2018 at 18:03
0

You can create your own custom function:

function ftp_is_directory($ftp_conn, $dir)
{
    $pushd = ftp_pwd($ftp_conn);

    if ($pushd !== false && @ftp_chdir($ftp_conn, $dir))
    {
        ftp_chdir($ftp_conn, $pushd);   
        return true;
    }

    return false;
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.