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
The bad thing about trying
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.
If you are connecting to one specific server, so you know its format of directory listing, 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).
The ideal solution is to use the
MLSD FTP command that returns a reliable machine-readable directory listing. But PHP does not support that.
There's an implementation of the
MLSD in user comments of the