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

How to Install PHP PEAR in Wamp on Windows ?

1. Open Command Prompt (cmd) or Go to Start and run write down cmd
2. Change your directory to where your php wampserver is currently install

Example

3. Now change your path to currently php version
Example

4. Now The command for installing pear is
—> go-pear.bat

Example

Now your pear is install

Thanks