Current Location: Home> Latest Articles> Implement FTP file browser using ftp_login + ftp_rawlist

Implement FTP file browser using ftp_login + ftp_rawlist

gitbox 2025-05-26

1. Connect and log in to the FTP server

First, use ftp_connect to establish a connection to the FTP server, and then log in with the ftp_login function.

 <?php
// connect FTP server
$ftp_server = "gitbox.net";
$conn_id = ftp_connect($ftp_server);

if (!$conn_id) {
    die("无法connect到 FTP server $ftp_server");
}

// Log in
$ftp_user = "your_username";
$ftp_pass = "your_password";
if (!ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    die("FTP Log in失败");
}

echo "成功connect并Log in到 $ftp_server\n";
?>

2. Get the directory list

After logging in successfully, we use ftp_rawlist to get the file and folder information in the directory.

 <?php
// Get the current directory list
$directory = "/";
$file_list = ftp_rawlist($conn_id, $directory);

if ($file_list === false) {
    die("Unable to get directory list");
}

// Output directory information
foreach ($file_list as $file) {
    echo $file . "<br>";
}
?>

Here is an array containing file details, similar to the output of the Linux ls -l command.


3. Parse the directory list

In order to make it easier to display, we need to parse the string returned by ftp_rawlist to distinguish files and directories.

 <?php
function parseFtpRawList($raw_list) {
    $items = [];
    foreach ($raw_list as $line) {
        $chunks = preg_split("/\s+/", $line, 9);
        if (count($chunks) === 9) {
            list($permissions, $number, $owner, $group, $size, $month, $day, $timeOrYear, $name) = $chunks;
            $items[] = [
                'permissions' => $permissions,
                'size' => $size,
                'name' => $name,
                'is_dir' => $permissions[0] === 'd',
                'raw' => $line,
            ];
        }
    }
    return $items;
}

$parsed_list = parseFtpRawList($file_list);
foreach ($parsed_list as $item) {
    echo ($item['is_dir'] ? "[DIR] " : "[FILE] ") . htmlspecialchars($item['name']) . "<br>";
}
?>

4. Implement simple browsing function

We can switch paths based on the directory name clicked by the user, and support recursive browsing.

 <?php
// pass GET Parameters to obtain the current directory
$current_dir = isset($_GET['dir']) ? $_GET['dir'] : "/";

// Avoid path crossing attacks
$current_dir = str_replace(["..", "//"], "", $current_dir);

$file_list = ftp_rawlist($conn_id, $current_dir);
$parsed_list = parseFtpRawList($file_list);

echo "<h2>Table of contents: " . htmlspecialchars($current_dir) . "</h2>";
echo "<ul>";

// 上级Table of contents链接
if ($current_dir !== "/") {
    $parent_dir = dirname($current_dir);
    if ($parent_dir == "\\") $parent_dir = "/";
    echo '<li><a href="?dir=' . urlencode($parent_dir) . '">[..]</a></li>';
}

foreach ($parsed_list as $item) {
    if ($item['is_dir']) {
        echo '<li>[DIR] <a href="?dir=' . urlencode(rtrim($current_dir, "/") . "/" . $item['name']) . '">' . htmlspecialchars($item['name']) . '</a></li>';
    } else {
        echo '<li>[FILE] ' . htmlspecialchars($item['name']) . '</li>';
    }
}
echo "</ul>";
?>

5. Disconnect FTP

After browsing, don't forget to close the connection.

 <?php
ftp_close($conn_id);
?>

Summarize

Through the above steps, we complete a simple FTP file browser based on PHP:

  • Connect and log in to the FTP server

  • Use ftp_rawlist to get directory content

  • Analyze and distinguish files from directories

  • Implement directory switching browsing

You can further expand functions based on this basis, such as file download, deletion, upload, etc. I hope this article can help you quickly get started with PHP FTP programming!