问题
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