PHP Delete Files & Folders Using FTP From Remote Server

Reading Time: 5 minutes

Inside this article we will understand PHP Delete files & folders using FTP from remote server. Article contains classified information about deleting files and folders from remote server by connecting with FTP.

This tutorial will guide you only to delete files and folders. File can be of any type and extension. To delete files and folders from remote server you need FTP details like hostname, username, password & filepath. You will understand the complete idea to delete files from remote server using PHP.

PHP provides several functions for FTP. We will use those functions and work.

The File Transfer Protocol is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network.

Learn More –

Let’s get started.

PHP – FTP Functions

Here, we will see few PHP FTP functions and their use.

  • ftp_connect()
  • ftp_login()
  • ftp_mlsd()
  • ftp_delete()
  • ftp_close()

To Establish FTP Connection

$ftp_connection = ftp_connect($ftp_server)

Login To FTP Server

$login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);

List All Files and Folders

$lists = ftp_mlsd($ftp_connection, $directory);

Delete a Folder From FTP Server

Folder should be empty to delete it.

ftp_delete($ftp_connection, $folderPath);

Delete a File From FTP Server

ftp_delete($ftp_connection, $file)

Close a FTP Connection


Let’s create a complete PHP application by using all above functions.

Create an Application

Create a folder php-ftp in your localhost directory.

Inside this folder, create a file called index.php. Open index.php and write this complete code into it.


// Connect to FTP server

// Assign ftp server to the variable
$ftp_server = "";

// Use correct ftp username
$ftp_username = "yyyyy";

// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass = "zzzzz";

// Folder contains subfolders and files into it
$filesAndFolders = "/public_html/yourfolder";

// Establishing ftp connection
$ftp_connection = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");

if ($ftp_connection) {
    echo "successfully connected to the ftp server!";

    // Logging in to established connection with
    // ftp username and password
    $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass);

    if ($login) {

        // Checking whether logged in successfully or not
        echo "<br>logged in successfully!";
        removeFtpFolders($ftp_connection, $filesAndFolders);
        echo "<br>Deleted!";
    } else {
        echo "<br>login failed!";

    // Closing connection
    if (ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";

function removeFtpFolders($ftp_connection, $directory)
    if (count(ftp_mlsd($ftp_connection, $directory)) <= 2) {
        // folder is empty
        ftp_rmdir($ftp_connection, $directory);
    } else {
        $lists = ftp_mlsd($ftp_connection, $directory);

        foreach ($lists as $key => $list) {
            $subFolderPath = $directory . '/' . $list['name'];
            // finding subfolders
            if ($list['type'] == 'dir') {
                removeFtpFolders($ftp_connection, $subFolderPath);
            } else {
                // delete file
                ftp_delete($ftp_connection, $subFolderPath);
    return true;

Application Testing

Open browser and hit this URL: http://localhost/php-ftp

When application executes, it will delete your remote files and folders.

We hope this article helped you to learn PHP Delete Files & Folders Using FTP From Remote Server Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more