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 //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

Bài Viết Liên Quan

Chủ Đề