[PHP] How To Connect To FTP Via PHP Functions

Hi Friends, Today I am going to explain you how to connect to FTP or how can we connect to FTP via PHP functions to perform some manipulations on files like copy file exists in one server to local server etc. My requirements was I have to connect to one FTP program via php functions and copy one file exists in one folder to my server and I have to do this via PHP script because this process will run every day via cron job. So I have searched about it found inbuilt PHP functions which helps me a lot in this.

So below is the code by which you can connect to FTP

$ftp_server = "ftp server/host"; //FTP Host Name
$ftp_user_name = "ftp username"; // FTP Username
$ftp_user_pass = "ftp password"; // FTP password
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

Now above is the code by which you can connect to any ftp server via PHP. This is basic code to connect FTP and if you want to do further operations after connecting with files then click here to know more functions about PHP FTP.

It is little bit tricky job so let me know if you need any help. I am PHP Freelancer and WordPress Developer So If you have any projects related to PHP or any Open Source then contact me.