删除父目录和所有子文件,然后重定向 [英] Delete parent directory and all child files then redirect

查看:47
本文介绍了删除父目录和所有子文件,然后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的页面结构:

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

页面/setup/index.php 上的表单如下:

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

提交该表单时,我希望删除目录/setup/以及其子级 index.php setup.php .之后,我需要它重定向到 domain.com/index.php

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

我尝试像这样使用 rmdir :

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.

推荐答案

rmdir()要求目录为空.您可以使用此功能删除文件夹中的文件,然后再删除文件夹.

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 );  
    }
}
?>

这篇关于删除父目录和所有子文件,然后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆