На современных FTP-серверах вы можете использовать команду MLST / MLSD для получения подробной машиночитаемой информации о файлах. Прочтите страницу RFC https://tools.ietf.org/html/rfc3659#page-23, чтобы узнать больше об этой команде.
Вот пример кода для определения типа узла файловой системы:
function isDir($ftp, $fsNodePath) {
$type = strtolower(fsNodeType($ftp, $fsNodePath));
return ($type === 'cdir' || $type === 'pdir' || $type === 'dir');
}
function isFile($ftp, $fsNodePath) {
$type = strtolower(fsNodeType($ftp, $fsNodePath));
return ($type === 'file');
}
function isLink($ftp, $fsNodePath) {
$type = strtolower(fsNodeType($ftp, $fsNodePath));
return (preg_match('/^OS\.unix\=(slink|symlink)/i', $type) === 1);
}
function fsNodeType($ftp, $fsNodePath)
{
$lines = array_values(ftp_raw($ftp, "MLST $fsNodePath"));
$linesCount = count($lines);
if ($linesCount === 1) {
throw new Exception('Unsuitable response for MLST command: ' . $lines[0]);
}
if ($linesCount !== 3) {
$e = new Exception('Unexpected response for MLST command (1)');
$e->response = $lines;
throw $e;
}
if (!preg_match('/^250\-/', $lines[0]) || !preg_match('/^250 /', $lines[2])) {
$e = new Exception('Unexpected response for MLST command (2)');
$e->response = $lines;
throw $e;
}
$spEntry = ' ' . $lines[1];
if (preg_match('/[\s\;]type\=([^\;]+)/i', $spEntry, $matches)) {
$type = trim($matches[1]);
return $type;
} else {
throw new Exception('Failed to extract filesystem node type from SP entry:' . $spEntry);
}
}
$ftp = ftp_connect('192.168.0.100');
ftp_login($ftp, 'user', '1234');
$is = isDir($ftp, 'tmp');
var_dump($is);
Обратите внимание, что не каждый сервер поддерживает команду MLST. Например, ftp.freebsd.org не делает: (