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 9152841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:09:11+00:00 2026-06-17T12:09:11+00:00

<?php function remove_directory($directory) { if (is_dir($directory) === true) { $contents = scandir($directory); unset($contents[0], $contents[1]);

  • 0
        <?php
        function remove_directory($directory) {
            if (is_dir($directory) === true) {
                $contents = scandir($directory);
                unset($contents[0], $contents[1]);

                foreach($contents as $object) {
                    $current_object = $directory.'/'.$object;
                    if (filetype($current_object) === 'dir') {
                        remove_directory($current_object);
                        } else {
                        unlink($current_object);    
                        }
                    }
                    rmdir($directory);
                }
            }

        ?>
        <h1>DELETE</h1>
        <form action="" method="get">
           <?php if (isset($_GET['delete']) === true) {
               remove_directory('files/folder'); / when I click that submit button must delete only 'folder' not all 'folder1' and 'folder2'
                }
            ?>
            <input type="submit" name="delete"/>
        </form>
        <form action="" method="get">
           <?php if (isset($_GET['delete']) === true) {
               remove_directory('files/folder1'); // when I click that submit button must delete only 'folder1'
                }
            ?>
            <input type="submit" name="delete"/>
        </form>
        <form action="" method="get">
           <?php if (isset($_GET['delete']) === true) {
               remove_directory('files/folder2');  // when I click that submit button must delete only 'folder2'
                }
            ?>
            <input type="submit" name="delete"/>
        </form>

When I click submit button I want delete only submitted file. But when I click submit button now it is delete all file in directory not onlu submitted…
When I click first button I must delete only ‘files/folder’ not all … How I can get that???? I have a function remove_directory. I know my proglem is with $_GET variable but i don’t know how I can fix it.
But now when I click just one submit button delete all folders – folder, folder1 and folder2
Where is the problem or how I can fix it?

  • 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-06-17T12:09:12+00:00Added an answer on June 17, 2026 at 12:09 pm

    PHP code is executed on the server, the created HTML is send to the client. Putting PHP code in your form doesn’t mean that that code will be executed on form submit. In fact it isn’t connected to the form at all.

    Instead you need to send which dir should be deleted in the form using an <input type="hidden">.

    Also I recommend using POST instead of GET.

        <?php
        function remove_directory($directory) {
            if (!is_dir($directory)) return;
    
            $contents = scandir($directory);
            unset($contents[0], $contents[1]);
    
            foreach($contents as $object) {
                $current_object = $directory.'/'.$object;
                if (filetype($current_object) === 'dir') {
                    remove_directory($current_object);
                } else {
                    unlink($current_object);    
                }
            }
    
            rmdir($directory);
        }
    
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['dir'])) {
            $dir = basename($_POST['dir']);
            if ($dir[0] != '.') remove_directory("files/$dir");
        }
    
        ?>
        <h1>DELETE</h1>
        <form action="" method="post">
            <input type="hidden" name="dir" value="folder" />
            <input type="submit" name="delete"/>
        </form>
        <form action="" method="post">
            <input type="hidden" name="dir" value="folder2" />
            <input type="submit" name="delete"/>
        </form>
        <form action="" method="post">
            <input type="hidden" name="dir" value="folder1" />
            <input type="submit" name="delete"/>
        </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function remove_directory($directory) { if (is_dir($directory) === true) { $contents = scandir($directory); unset($contents[0], $contents[1]); foreach($contents
I'm trying to use a PHP function to read a server directory for images.
Calls to PHP's is_readable() function are returning false on a directory that is readable
Is there php function to remove the space inside the string? for example: $abcd=this
http://php.net/manual/en/function.trim.php The manual entry does say that I can remove any specified leading or
MY PHP FUNCTION IS function functionName() { include($_SERVER['DOCUMENT_ROOT']./path/file.php); } Content of File.php is $foo
I have a PHP function that extract dat of an invoice from DB. The
I have a php function which returns JSON in response to a ajax request
I've written a PHP function to get plus ones count for a URL function
I found a php function checkdate() , but strangely enough it only seems to

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.