Transfer a file over a secure FTP connection with Curl

curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP).
With help of php curl we can transfer a data from one connection to another connection there for we need to create code snippet like this.

<?php 
     $username = your_ftp_username; 
     $password = 'your_ftp_password'; 
     $url = 'example.com’; 
     $ftp_server = "ftp://" . $username . ":" . $password . "@" . $url; 
     
     echo "initializing CURL.\n"; 
     $ch = curl_init(); 
     echo "Set CURL URL.\n"; 
     
     //curl FTP 
     curl_setopt($ch, CURLOPT_URL, $ftp_server); 
     
     //Remove slashes for  Debugging 
     //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);    
     
     // Set SSL Settings 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
     curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); 
     
     //List FTP files and directories (with only display list) 
     curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE); 
     
     //Output to curl_exec 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     echo "Executing CURL.\n"; 
     $output = curl_exec($ch); 
     curl_close($ch); 
     echo "Closing CURL.\n"; 
     echo  $output . "\n"; 
   // List output result
    $files = explode("\n", $output); 
    print_r($files); 
?>
Advertisements