PHP Delete Files & Folders Using FTP From Remote Server

Share this Article
Reading Time: 5 minutes
540 Views

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

ftp_close($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.

<?php

// Connect to FTP server

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

// 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5