ftp server using php
0 0
Read Time:2 Minute, 51 Second

In this blog, we are going to learn how to connect to the FTP server and handle files using PHP.

There are several FTP clients available for managing FTP. But if you want to handle files on the FTP with a script, you can do it with PHP.

Connect and log in to the FTP server

PHP provides ftp_connect() Function To connect to the FTP server And ftp_login() Function to Log in to the FTP server after a connection is established. And to close the FTP connection PHP provides ftp_close() Function.

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.

<?php
// FTP details
$ftpHost   = 'ftp.example.com';
$ftpUsername = 'ftpuser';
$ftpPassword = '*****';

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

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

// close the connection
ftp_close($conn);
?>

Upload File to the FTP

To Put files on the FTP, PHP provides ftp_put() Function.

After logging in to the FTP server, use ftp_put() function to upload files on the server.

<?php
// FTP details
$ftpHost   = 'ftp.example.com';
$ftpUsername = 'ftpuser';
$ftpPassword = '*****';

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

// login to FTP server
$ftpLogin = ftp_login($conn, $ftpUsername, $ftpPassword);

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

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

// close the connection
ftp_close($conn);
?>

Download File from the FTP server

To Download files From the FTP, PHP provides ftp_get() Function.

After logging in to the FTP server, use ftp_get() function to Download files From the server.

<?php
// FTP details
$ftpHost   = 'ftp.example.com';
$ftpUsername = 'ftpuser';
$ftpPassword = '*****';

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

// login to FTP server
$ftpLogin = ftp_login($conn, $ftpUsername, $ftpPassword);

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

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

// close the connection
ftp_close($conn);
?>

Delete File on the FTP Server

To Delete files From the FTP, PHP provides ftp_delete() Function.

After logging in to the FTP server, use ftp_delete() function to Delete files From the server.

<?php
// FTP details
$ftpHost   = 'ftp.example.com';
$ftpUsername = 'ftpuser';
$ftpPassword = '*****';

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

// login to FTP server
$ftpLogin = ftp_login($conn, $ftpUsername, $ftpPassword);

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

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

// close the connection
ftp_close($conn);
?>

Connect and Handle Files in FTP Server using PHP.zip

FREE DOWNLOAD

Send download link to:

Also Checkout: Send EMails In PHP Using PHPMailer

If You Have Any Problem Or Doubts,
Please Do Ask Or Suggest In Comment.


Do Follow Us on:

Facebook.com

Twitter.com

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

By Akash Kothari

I am passionate about my work. Because I love what I do, I have a steady source of motivation that drives me to do my best. In my last job, this passion led me to challenge myself daily and learn new skills that helped me to do better work

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “How to Connect and Handle Files in FTP Server using PHP?

Leave a Reply

%d bloggers like this: