Current Location: Home> Latest Articles> How to Securely Download Encrypted FTP Files Using the ftp_nb_get Function in PHP

How to Securely Download Encrypted FTP Files Using the ftp_nb_get Function in PHP

M66 2025-06-22

In PHP, the ftp_nb_get function is a highly useful tool for non-blocking FTP file downloads. Unlike the blocking ftp_get function, ftp_nb_get allows you to perform other operations during the download process, thus improving the responsiveness and efficiency of your program. However, when dealing with encrypted FTP files, additional measures must be taken to ensure secure downloads while maintaining file integrity and confidentiality.

This article will detail how to use the PHP ftp_nb_get function to securely download encrypted FTP files, with a focus on the following topics:

  • Non-blocking download flow using ftp_nb_get

  • Securing FTP transmissions with SSL/TLS

  • Decrypting downloaded files

  • Preventing data tampering and handling errors


1. Introduction to Non-blocking Downloads

ftp_nb_get is the non-blocking download function in PHP's FTP extension. Its basic usage is as follows:

<?php
$conn = ftp_connect("m66.net");
ftp_login($conn, "username", "password");
<p>$local_file = "local_encrypted_file.dat";<br>
$remote_file = "remote_encrypted_file.dat";</p>
<p>$ret = ftp_nb_get($conn, $local_file, $remote_file, FTP_BINARY);<br>
while ($ret == FTP_MOREDATA) {<br>
// You can perform other operations here<br>
$ret = ftp_nb_continue($conn);<br>
}</p>
<p>if ($ret == FTP_FINISHED) {<br>
echo "Download complete\n";<br>
} else {<br>
echo "Download failed\n";<br>
}</p>
<p>ftp_close($conn);<br>
?><br>

The code above demonstrates the basic non-blocking download flow. The key is to poll ftp_nb_continue to continue the download until it's finished.


2. Securing FTP Transmissions

Standard FTP transmissions are sent in plaintext, making files vulnerable to interception during transfer. To securely download encrypted FTP files, the transmission itself must be encrypted. Here are two common methods:

1. Using FTPS (FTP over SSL/TLS)

PHP's FTP extension supports FTPS. You can use ftp_ssl_connect instead of ftp_connect:

<?php
$conn = ftp_ssl_connect("m66.net"); // Connect to FTP server via SSL
ftp_login($conn, "username", "password");
<p>// Other operations remain the same<br>
?><br>

In this case, the transfer process is encrypted with SSL/TLS, preventing man-in-the-middle attacks.

2. Using SFTP

SFTP is a file transfer protocol based on SSH, offering enhanced security. However, the native PHP FTP extension does not support SFTP, so you'll need to use the ssh2 extension or a third-party library. This is beyond the scope of this article.


3. Decrypting the Downloaded Files

If the file on the server is encrypted (e.g., using AES symmetric encryption), it needs to be decrypted after downloading. The following example uses OpenSSL:

<?php
$encrypted_file = "local_encrypted_file.dat";
$decrypted_file = "local_decrypted_file.dat";
$key = "your-secret-key-123";  // Symmetric key
$iv = "your-16byteivvector";   // Initialization vector
<p>$encrypted_data = file_get_contents($encrypted_file);<br>
$decrypted_data = openssl_decrypt($encrypted_data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);</p>
<p>if ($decrypted_data === false) {<br>
die("Decryption failed");<br>
}</p>
<p>file_put_contents($decrypted_file, $decrypted_data);<br>
echo "File successfully decrypted\n";<br>
?><br>

Note:

  • The key and IV must match those used during encryption.

  • Key management must be secure—do not store keys in plaintext.


4. Complete Example: Non-blocking Secure Download and Decryption

<?php
$conn = ftp_ssl_connect("m66.net");
if (!$conn) {
    die("Unable to connect to FTP server");
}
<p>if (!ftp_login($conn, "username", "password")) {<br>
ftp_close($conn);<br>
die("Login failed");<br>
}</p>
<p>ftp_pasv($conn, true);  // Enable passive mode, better suited for firewall environments</p>
<p>$local_encrypted = "local_encrypted_file.dat";<br>
$remote_encrypted = "remote_encrypted_file.dat";</p>
<p>$ret = ftp_nb_get($conn, $local_encrypted, $remote_encrypted, FTP_BINARY);<br>
while ($ret == FTP_MOREDATA) {<br>
// You can add progress displays or other logic here<br>
$ret = ftp_nb_continue($conn);<br>
}</p>
<p>if ($ret != FTP_FINISHED) {<br>
ftp_close($conn);<br>
die("Download failed");<br>
}</p>
<p>ftp_close($conn);<br>
echo "Download complete, starting decryption...\n";</p>
<p>// Decryption parameters<br>
$key = "your-secret-key-123";<br>
$iv = "your-16byteivvector";</p>
<p>$encrypted_data = file_get_contents($local_encrypted);<br>
$decrypted_data = openssl_decrypt($encrypted_data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);</p>
<p>if ($decrypted_data === false) {<br>
die("Decryption failed");<br>
}</p>
<p>$local_decrypted = "local_decrypted_file.dat";<br>
file_put_contents($local_decrypted, $decrypted_data);</p>
<p>echo "File successfully decrypted and saved to $local_decrypted\n";<br>
?><br>


5. Conclusion and Key Considerations

  • Use ftp_ssl_connect to secure transmissions and prevent man-in-the-middle attacks.

  • Use ftp_nb_get for non-blocking downloads to enhance efficiency and user experience.

  • Use OpenSSL to decrypt files after download, ensuring data confidentiality.

  • Properly manage keys and initialization vectors to prevent leakage.

  • Enable passive mode (ftp_pasv) to resolve compatibility issues with certain network environments.

With these techniques, you can securely and reliably download and process encrypted FTP files using PHP.