[PHP] How To Connect To FTP Via PHP Functions

Posted by Ankur Gandhi in PHP



php

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.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

  • http://www.qorser.com/ PHP Developer

    Thanks Ankur we will save you blog post for seeing another useful post

  • http://www.hire-phpdeveloper.com/ Hire PHP Developer

    Hi Ankur, I am PHP Developer and developing a web application which need to connect with File transfer protocol of website. I got code on how to connect ftp using PHP but how to read which files are folders present on ftp server??? Please advocate me. Thanks in advance.

  • http://www.hire-phpdeveloper.com/ Hire PHP Developer

    Hi Ankur, I am PHP Developer and developing a web application which need to connect with File transfer protocol of website. I got code on how to connect ftp using PHP but how to read which files are folders present on ftp server??? Please advocate me. Thanks in advance.