Delete parent directory and all child files then redirect

。_饼干妹妹 提交于 2019-12-22 17:04:10

问题


I have a page structure like this:

domain.com/index.php
domain.com/setup/index.php
domain.com/setup/setup.php

There's a form on the page /setup/index.php that looks like this:

<form action="" method="post">
  <input type="submit" name="finishSubmit" value="Finish" />
</form>

When that form is submitted, I want the directory /setup/ to be deleted, along with it's children index.php and setup.php. After that, I need it to redirect to domain.com/index.php

I tried using rmdir like this:

if (isset($_POST['finishSubmit'])) {
  rmdir('../setup');
  header ('location: ../');
}

Either I'm using it wrong or I'm getting the paths wrong, but it's not working. What is the correct way to do this?

NOTE: I need to use relative paths such as above because I won't know the domain name.


回答1:


rmdir() requires the directory to be empty. You can use this function to delete the files in the folder, then remove the folder after.

<?PHP
function delete_files($target) {
    if(is_dir($target)){
        $files = glob( $target . '*', GLOB_MARK );
        foreach( $files as $file )
        {
            delete_files( $file );      
        }
        rmdir( $target );
    } elseif(is_file($target)) {
        unlink( $target );  
    }
}
?>



回答2:


The PHP rmdir function only works for empty directories.

You would need to delete the index.php file and an other files first before removing the directory.

Reference: http://php.net/manual/en/function.rmdir.php



来源:https://stackoverflow.com/questions/19137925/delete-parent-directory-and-all-child-files-then-redirect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!