Are there alternative methods to upload files to an FTP server from a web page using PHP?
One alternative method to upload files to an FTP server from a web page using PHP is to use cURL to transfer the file. This can be achieved by reading the file contents and then sending it to the FTP server using cURL functions.
<?php
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$file_path = 'path/to/upload/file.txt';
$remote_file = 'file.txt';
$ch = curl_init();
$fp = fopen($file_path, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$ftp_user:$ftp_pass@$ftp_server/$remote_file");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Keywords
Related Questions
- Are there any best practices for handling date and time functions in PHP to avoid errors?
- What best practices should be followed when creating a navigation system in PHP to ensure better user experience and code organization?
- Is it recommended to use abstract classes solely to prevent instantiation, or are there better alternatives?