Hướng dẫn php ftp check if directory exists - php ftp kiểm tra xem thư mục có tồn tại không

Trên máy chủ FTP hiện đại, bạn có thể sử dụng lệnh MLST/MLSD để truy xuất thông tin có thể đọc được máy chi tiết về các tệp. Đọc trang RFC https://www.rfc-editor.org/rfc/rfc3659#page-23 để tìm hiểu thêm về lệnh này.

Dưới đây là mã mẫu để xác định loại nút hệ thống tập tin:

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);

Lưu ý rằng không phải mọi máy chủ đều hỗ trợ lệnh MLST. Ví dụ, ftp.freebsd.org không :(

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

// Chi tiết đăng nhập FTP
$ ftp_server = 'yourserver.com';ftp_server = 'yourserver.com';
$ ftp_server_path = '/public_html/';ftp_server_path = '/public_html/';
$ ftp_user_name = 'tên người dùng';ftp_user_name = 'username';
$ ftp_user_pass = 'mật khẩu';ftp_user_pass = 'password';
// Kết nối với FTP
$ Conn_id = ftp_connect ($ ftp_server);conn_id = ftp_connect($ftp_server);
$ login_result = ftp_login ($ Conn_id, $ ftp_user_name, $ ftp_user_pass);login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Liệt kê các tệp trong thư mục này (không đệ quy)
$ list = ftp_nlist ($ Conn_id, $ ftp_server_path);list = ftp_nlist($conn_id, $ftp_server_path);
// Các mục nhập vòng lặp
foreach ($ listas $ entry) {($list as $entry) {
// Phớt lờ '.'và '..', các phiên bản phức tạp hơn có thể nằm trong một
// mảng hoặc thậm chí một tệp văn bản ala .gitignore
if ($ entry! = '.' && $ entry! = '..') {($entry != '.' && $entry != '..') {
// Sử dụng chức năng kiểm tra của chúng tôi
$ is_dir = ftp_isdir ($ Conn_id, $ ftp_server_path. $ entry);is_dir = ftp_isdir($conn_id, $ftp_server_path . $entry);
// Kết quả nhật ký
if ($ is_dir)($is_dir)
echo "$ Entry là một thư mục n"; "$entry is a directory n";
else
echo "$ Entry là một tập tin n"; "$entry is a file n";
}
}
// Đóng kết nối
ftp_close ($ Conn_id);$conn_id);
// Hàm IS_DIR của chúng tôi, lưu ý @ được sử dụng để đàn áp các cảnh báo vô dụng
functionftp_isdir ($ Conn_id, $ dir) ftp_isdir($conn_id,$dir)
{
if (@ftp_chdir ($ Conn_id, $ dir)) {(@ftp_chdir($conn_id,$dir)) {
ftp_cdup ($ Conn_id);$conn_id);
trở lại; true;
} khác {else {
returnFalse; false;
}
}