What are the differences between using FTP functions and cURL for file transfers in PHP?
When it comes to file transfers in PHP, FTP functions and cURL are two common options. FTP functions are built-in PHP functions specifically designed for working with FTP servers, making them a straightforward choice for FTP transfers. On the other hand, cURL is a more versatile library that supports various protocols, including FTP, making it a more flexible option for file transfers.
// Using FTP functions for file transfer
$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";
$file_to_upload = "local_file.txt";
$remote_file = "remote_file.txt";
$ftp_conn = ftp_connect($ftp_server);
ftp_login($ftp_conn, $ftp_user, $ftp_pass);
ftp_put($ftp_conn, $remote_file, $file_to_upload, FTP_ASCII);
ftp_close($ftp_conn);
```
```php
// Using cURL for file transfer
$remote_url = "ftp://username:password@ftp.example.com/remote_file.txt";
$file_to_upload = "local_file.txt";
$ch = curl_init();
$fp = fopen($file_to_upload, 'r');
curl_setopt($ch, CURLOPT_URL, $remote_url);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_FTPASCII, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);