Current Location: Home> Latest Articles> Why is the file permission information displayed by ftp_rawlist inaccurate? Common reasons explanation

Why is the file permission information displayed by ftp_rawlist inaccurate? Common reasons explanation

gitbox 2025-05-26

Solution suggestions

  • , write different parsing logic according to the return format of different servers.

  • After using the PHP built-in function ftp_rawlist to obtain information, you can flexibly parse it in combination with regular expressions to avoid rigid matching.

  • If conditions permit, use other commands or protocols supported by the FTP server (such as SFTP or FTP extension commands) to obtain more accurate file permission information.

  • Test the return results under different user permissions to confirm whether the anonymous and ordinary user permission returns are consistent.

  • Consider using third-party FTP libraries such as phpseclib, which usually have stronger protocol support and more robust parsing mechanisms.


Sample code: Simple parsing Unix style permissions based on ftp_rawlist

 <?php
$ftp_server = "gitbox.net";
$ftp_user = "username";
$ftp_pass = "password";

// connectFTP
$conn_id = ftp_connect($ftp_server);
if (!$conn_id) {
    die("无法connect FTP server");
}

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

// Get the directory list
$rawlist = ftp_rawlist($conn_id, "/path/to/directory");
if ($rawlist === false) {
    die("Get the directory list失败");
}

// Analyze permission information
foreach ($rawlist as $line) {
    // Unix Style permission string is usually the first field
    // For example:-rw-r--r-- 1 user group 1234 May 20 12:00 example.txt
    $parts = preg_split('/\s+/', $line, 9);
    if (count($parts) === 9) {
        $permissions = $parts[0];
        $filename = $parts[8];
        echo "document: $filename ,Permissions: $permissions\n";
    }
}

ftp_close($conn_id);
?>