Connect and Handle Files in FTP Server using PHP

Upload file to the server via FTP is an essential task for every web developers. There are many FTP clients are available for handling files on FTP server. But if you want to connect FTP server by the script, you can use PHP to handle files in FTP server.

PHP provides various functions to work with FTP server. In this tutorial, show you how to connect FTP server using PHP and the most required functionality to handling files on FTP server with PHP.

Connect and Login to the FTP Server

At first, connect to the FTP server using ftp_connect() function. Once the FTP connection is created, use ftp_login() function to login to the FTP server by FTP username and password.

// FTP server details
$ftpHost   'ftp.example.com';
$ftpUsername 'ftpuser';
$ftpPassword '*****';

// open an FTP connection
$connId ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");

// try to login
if(@ftp_login($connId$ftpUsername$ftpPassword)){
    echo 
"Connected as $ftpUsername@$ftpHost";
}else{
    echo 
"Couldn't connect as $ftpUsername";
}

// close the connection
ftp_close($connId);

Upload File to the FTP Server

After logged in to the FTP server, use ftp_put() function to upload file on the server.

// FTP server details
$ftpHost   'ftp.example.com';
$ftpUsername 'ftpuser';
$ftpPassword '*****';

// open an FTP connection
$connId ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");

// login to FTP server
$ftpLogin ftp_login($connId$ftpUsername$ftpPassword);

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

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

// close the connection
ftp_close($connId);

Download File from the FTP Server

After logged in to the FTP server, use ftp_get() function to download the file from the server.

// FTP server details
$ftpHost   'ftp.example.com';
$ftpUsername 'ftpuser';
$ftpPassword '*****';

// open an FTP connection
$connId ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");

// login to FTP server
$ftpLogin ftp_login($connId$ftpUsername$ftpPassword);

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

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

// close the connection
ftp_close($connId);

Delete File on the FTP Server

After logged in to the FTP server, use ftp_delete() function to delete a file on the server.

// FTP server details
$ftpHost   'ftp.example.com';
$ftpUsername 'ftpuser';
$ftpPassword '*****';

// open an FTP connection
$connId ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");

// login to FTP server
$ftpLogin ftp_login($connId$ftpUsername$ftpPassword);

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

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

// close the connection
ftp_close($connId);

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up