How can the use of ftp_pasv() function in PHP help resolve issues related to passive file uploads?

Passive file uploads can sometimes fail due to network configurations or firewall settings blocking the data connection. Using the ftp_pasv() function in PHP can help resolve this issue by switching the FTP connection to passive mode, allowing the server to initiate the data connection.

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

// Switch to passive mode
ftp_pasv($conn_id, true);

// Upload file
$file = 'local_file.txt';
$remote_file = 'remote_file.txt';
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "File uploaded successfully";
} else {
    echo "Failed to upload file";
}

// Close FTP connection
ftp_close($conn_id);