Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:05:49+00:00 2026-05-27T19:05:49+00:00

Trying to add the ability to delete a Folder using FTP and all subfolders

  • 0

Trying to add the ability to delete a Folder using FTP and all subfolders and files contained within that folder.

I have built a recursive function to do so, and I feel like the logic is right, but still doesnt work.

I did some testing, I am able to delete on first run if the path is just an empty folder or just a file, but can’t delete if it is a folder containing one file or a folder containing one empty subfolder. So it seems to be a problem with traversing through the folder(s) and using the function to delete.

Any ideas?

function ftpDelete($directory)
{
    if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders
        return json_encode(false);
    else{
        global $conn_id;
        # here we attempt to delete the file/directory
        if( !(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory)) )
        {
            # if the attempt to delete fails, get the file listing
            $filelist = @ftp_nlist($conn_id, $directory);

            # loop through the file list and recursively delete the FILE in the list
            foreach($filelist as $file)
                ftpDelete($file);

            #if the file list is empty, delete the DIRECTORY we passed
            ftpDelete($directory);
        }
        else
            return json_encode(true);
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:05:50+00:00Added an answer on May 27, 2026 at 7:05 pm

    Ok found my problem. Since I wasn’t moving into the exact directory I was trying to delete, the path for each recursive file being called wasn’t absolute:

    function ftpDeleteDirectory($directory)
    {
        global $conn_id;
        if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders
            return json_encode(false);
        else{
            # here we attempt to delete the file/directory
            if( !(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory)) )
            {
                # if the attempt to delete fails, get the file listing
                $filelist = @ftp_nlist($conn_id, $directory);
                # loop through the file list and recursively delete the FILE in the list
                foreach($filelist as $file)
                {
                //  return json_encode($filelist);
                    ftpDeleteDirectory($directory.'/'.$file);/***THIS IS WHERE I MUST RESEND ABSOLUTE PATH TO FILE***/
                }
    
                #if the file list is empty, delete the DIRECTORY we passed
                ftpDeleteDirectory($directory);
            }
        }
        return json_encode(true);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MS Visual C++ and Boost asio, I'm trying to add the ability to
I have a WinForms app and am trying to add the ability to start
I'm am trying to create a jQuery plugin that will add new namespace functions
I am trying to add the ability for the camera to go up and
I have an events form that allows someone to add a new event. An
I have a simple reader app on the app store that I am trying
I'm trying to create a HTTP handler to handle all requests to a folder
I am trying to add some administration scripts to a Zend Framework project, that
I am using NHibernate in a project, and I am trying to add it
I am trying to do some databinding magic. I have a Shipments view that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.