What are the best practices for transferring PHP files to a web server via FTP?

When transferring PHP files to a web server via FTP, it is important to ensure that the files are uploaded securely and without any corruption. To do this, make sure to use a secure FTP connection (SFTP) and transfer the files in binary mode to prevent any line ending conversions. Additionally, always check the file permissions on the server to ensure that the PHP files are executable.

// Example PHP code for transferring files to a web server via FTP
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$local_file = "local_file.php";
$remote_file = "remote_file.php";

// Connect to FTP server
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

// Login to FTP server
ftp_login($ftp_conn, $ftp_username, $ftp_password);

// Set transfer mode to binary
ftp_pasv($ftp_conn, true);
ftp_set_option($ftp_conn, FTP_BINARY, true);

// Transfer file to server
if (ftp_put($ftp_conn, $remote_file, $local_file, FTP_BINARY)) {
    echo "File $local_file successfully uploaded to $ftp_server as $remote_file";
} else {
    echo "Error uploading file";
}

// Close FTP connection
ftp_close($ftp_conn);