Skip to content

Instantly share code, notes, and snippets.

@kostiantyn-petlia
Created November 27, 2018 13:49
Show Gist options
  • Save kostiantyn-petlia/a56634565b7e26efa012467ccff915b0 to your computer and use it in GitHub Desktop.
Save kostiantyn-petlia/a56634565b7e26efa012467ccff915b0 to your computer and use it in GitHub Desktop.
Deleting all files from a folder using PHP
<?php
/**
* !!! BE CAREFULLY !!!
*
* Will be deleted all files in the current dir!
* Place this PHP-script file in the target folder & run like http://site.com/some-target-folder/delete.php
*
* Author: K
* Version: 2.0
*
*/
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete all files in the current dir</title>
</head>
<body style="padding: 2rem;">
<h1>Delete all files in the current dir</h1>
<h3>Note: the scrypt file must be placed in the target folder.</h3>
<?php
// Print here current target dir
$target_folder = ( isset( $_GET['folder'] ) && ! empty( $_GET['folder'] ) ) ? $_GET['folder'] : null; // Delete all files & folders in this folder (name)
$exceptions = [];
$exceptions_results = [];
if ( ! empty( $_GET['exceptions'] ) ) {
$exceptions = explode( ',', trim( preg_replace( '/\s+/', '', $_GET['exceptions'] ) ) );
}
$current_file_name = basename( __FILE__ );
$current_dir = dirname( __FILE__ ); // path/to/directory === Current File Dir
?>
<script>
// Remove GET params form URL
var clearURL = location.href.split("?")[0];
window.history.replaceState({}, document.title, clearURL);
</script>
<p>You are here: <?php echo $current_dir; ?>\<strong><?php echo $current_file_name; ?></strong>
</p>
<hr>
<form action="" style="margin: 3rem 0;">
<p>
<label for="confirm"><strong>Specify the target folder name where is this script file:</strong></label><br>
<input type="text" name="folder" value="" placeholder="this-folder-will-be-empty" required style="width: 100%;">
</p>
<p>
<label for="exceptions"><strong>Don't delete files/folders with these path entries:</strong></label><br>
<input type="text" name="exceptions" value="" placeholder="foo, bar.txt, /ignore-this/too" style="width: 100%;">
</p>
<p>
<label for="confirm"><strong>Confirm the irreversible action!</strong></label><br>
<input type="checkbox" name="confirm" value="confirmed">I understand that all files will be deleted<br>
</p>
<input type="submit" role="button">
</form>
<hr>
<?php if ( $target_folder === basename( $current_dir ) && isset( $_GET['confirm'] ) ) : // Delete only if the target_dir is the current_dir ?>
<?php
$di = new RecursiveDirectoryIterator( $current_dir, FilesystemIterator::SKIP_DOTS );
$ri = new RecursiveIteratorIterator( $di, RecursiveIteratorIterator::CHILD_FIRST );
foreach ( $ri as $file ) {
// Check the target folder in the path to file (simple safety)
// And don't delete this php-file. (Note: $file->getPath(); $file->getPathname(); $file->getFilename();)
if ( strpos( $file->getPath(), $target_folder ) && $current_file_name != $file->getFilename() ) {
// Check exceptions in the target folder (any entry in the filepath)
$is_exception = false;
foreach ( $exceptions as $exception ) {
if ( false !== strpos( $file->getPathname(), $exception ) ) {
$is_exception = true;
$exceptions_results[] = str_replace( $exception, '<strong style="color:red;">' . $exception . '</strong>', $file->getPathname() );
break;
}
}
// Delete dir or file
if ( ! $is_exception ) {
// Empty dir ('.' and '..')
if ( $file->isDir() && count( scandir( $file->getPathname() ) ) == 2 ) {
// delete the dir
rmdir( $file );
} else if ( ! $file->isDir() ) {
// delete the file
unlink( $file );
}
}
}
}
?>
<h2>Result</h2>
<p style="color:green;">
<strong><?php echo 'Files in the target folder were deleted!'; ?></strong>
</p>
<h3>Exception entries:</h3>
<ul>
<?php foreach ( $exceptions_results as $item ) : ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<h3>Current directory files:</h3>
<ul>
<?php $list = scandir( $current_dir ); ?>
<?php foreach ( $list as $item ) : ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>
<strong style="color:red;">Nothing was deleted in:</strong> <?php echo $current_dir; ?>
</p>
<?php endif; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment