How can PHP scripts be adapted to work with FTP functions to ensure proper file ownership and permissions?

When working with FTP functions in PHP, it's important to ensure that the proper file ownership and permissions are set for files that are uploaded or modified. This can be done by using the FTP functions to change the ownership and permissions of the files after they have been uploaded. By using the `ftp_chown()` and `ftp_chmod()` functions, you can specify the desired ownership and permissions for the files.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_connection = ftp_connect($ftp_server);
ftp_login($ftp_connection, $ftp_user, $ftp_pass);

// Upload file
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
ftp_put($ftp_connection, $remote_file, $local_file, FTP_BINARY);

// Set file ownership and permissions
$owner = 'ftpuser';
$file_mode = 0644;
ftp_chown($ftp_connection, $remote_file, $owner);
ftp_chmod($ftp_connection, $file_mode, $remote_file);

// Close FTP connection
ftp_close($ftp_connection);