Connect and Handle Files in FTP Server using PHP

Connect and Handle Files in FTP Server using PHPIt is important for any web developers for upload files to server using FTP.
We know there are plenty of FTP clients available in market like filezilla, cute ftp etc .
PHP also provide lots of functions to handle files on FTP server . In this below PHP tutorial we will guide you how to use PHP scripts to handle files in FTP server .

Connect and Login to the FTP Server

First of all we have to connect to ftp server using a PHP function i.e ftp_connect() . After connection we have to login to ftp server using ftp_login() function providing username and password . Find below it’s code .

 // FTP server details
$ftp_host   = 'ftp.example.com';
$ftp_username = 'ftpuser';
$ftp_password = '*****';

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");

// try to login
if(@ftp_login($conn_id, $ftp_username, $ftp_password)){
    echo "Connected as $ftp_username@$ftp_host";
}else{
    echo "Couldn't connect as $ftp_username";
}

// close the connection
ftp_close($conn_id);
 

Upload File to the FTP Server

Now time to upload files to the server. We have to use function ftp_put() to do so.

// FTP server details
$ftp_host   = 'ftp.example.com';
$ftp_username = 'ftpuser';
$ftp_password = '*****';

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");

// login to FTP server
$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);

// local & server file path
$localFilePath  = 'index.php';
$remoteFilePath = 'public_html/index.php';

// try to upload file
if(ftp_put($conn_id, $remoteFilePath, $localFilePath, FTP_ASCII)){
    echo "File transfer successful - $localFilePath";
}else{
    echo "There was an error while uploading $localFilePath";
}

// close the connection
ftp_close($conn_id);

Download File from the FTP Server

Most of the time ,we have requirement to download files from the server. For this we have to use PHP function ftp_get() .

// FTP server details
$ftp_host   = 'ftp.example.com';
$ftp_username = 'ftpuser';
$ftp_password = '*****';

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");

// login to FTP server
$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);

// local & server file path
$localFilePath  = 'index.php';
$remoteFilePath = 'public_html/index.php';

// try to download a file from server
if(ftp_get($conn_id, $localFilePath, $remoteFilePath, FTP_BINARY)){
    echo "File transfer successful - $localFilePath";
}else{
    echo "There was an error while downloading $localFilePath";
}

// close the connection
ftp_close($conn_id);

Delete File on the FTP Server

Finally let’s do to delete file from the server . PHP has a function to do this task i.e ftp_delete() .

// FTP server details
$ftp_host   = 'ftp.example.com';
$ftp_username = 'ftpuser';
$ftp_password = '*****';

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("Couldn't connect to $ftp_host");

// login to FTP server
$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);

// server file path
$file = 'public_html/index_old.php';

// try to delete file on server
if(ftp_delete($conn_id, $file)){
    echo "$file deleted successful";
}else{
    echo "There was an error while deleting $file";
}

// close the connection
ftp_close($conn_id);

Online Web Development Training,Video Tutorials