What are the best practices for creating FTP accounts for users on a web server running PHP?

When creating FTP accounts for users on a web server running PHP, it is important to ensure that each user has their own unique account with restricted access to their designated directories. This helps maintain security and prevent unauthorized access to sensitive files on the server. One way to achieve this is by using PHP to automate the creation of FTP accounts for users, assigning them to specific directories and setting permissions accordingly.

// Create FTP account for a user with restricted access to a specific directory
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_dir = '/path/to/directory/';

// Connect to FTP server
$ftp_conn = ftp_connect($ftp_server);
ftp_login($ftp_conn, $ftp_user, $ftp_pass);

// Create user directory if it doesn't exist
if (!ftp_chdir($ftp_conn, $ftp_dir)) {
    ftp_mkdir($ftp_conn, $ftp_dir);
}

// Set directory permissions
ftp_chmod($ftp_conn, 0755, $ftp_dir);

// Close FTP connection
ftp_close($ftp_conn);