remove chunk folder php RecursiveDirectoryIterator
This commit is contained in:
@@ -18,7 +18,7 @@ if (!isset($_POST['folder'])) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$folder = $_POST['folder'];
|
$folder = $_POST['folder'];
|
||||||
// Validate the folder name to allow only expected characters (adjust the regex as needed)
|
// Validate the folder name (only alphanumerics, dashes allowed)
|
||||||
if (!preg_match('/^resumable_[A-Za-z0-9\-]+$/', $folder)) {
|
if (!preg_match('/^resumable_[A-Za-z0-9\-]+$/', $folder)) {
|
||||||
echo json_encode(["error" => "Invalid folder name"]);
|
echo json_encode(["error" => "Invalid folder name"]);
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
@@ -33,27 +33,28 @@ if (!is_dir($tempDir)) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively delete directory and its contents
|
// Recursively delete directory using RecursiveDirectoryIterator
|
||||||
function rrmdir($dir) {
|
function rrmdir($dir) {
|
||||||
if (is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
$objects = scandir($dir);
|
return;
|
||||||
foreach ($objects as $object) {
|
|
||||||
if ($object != "." && $object != "..") {
|
|
||||||
$path = $dir . DIRECTORY_SEPARATOR . $object;
|
|
||||||
if (is_dir($path) && !is_link($path)) {
|
|
||||||
rrmdir($path);
|
|
||||||
} else {
|
|
||||||
@unlink($path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@rmdir($dir);
|
|
||||||
}
|
}
|
||||||
|
$it = new RecursiveIteratorIterator(
|
||||||
|
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
|
||||||
|
RecursiveIteratorIterator::CHILD_FIRST
|
||||||
|
);
|
||||||
|
foreach ($it as $file) {
|
||||||
|
if ($file->isDir()){
|
||||||
|
rmdir($file->getRealPath());
|
||||||
|
} else {
|
||||||
|
unlink($file->getRealPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rmdir($dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
rrmdir($tempDir);
|
rrmdir($tempDir);
|
||||||
|
|
||||||
// check if folder still exists
|
// Verify removal
|
||||||
if (!is_dir($tempDir)) {
|
if (!is_dir($tempDir)) {
|
||||||
echo json_encode(["success" => true, "message" => "Temporary folder removed."]);
|
echo json_encode(["success" => true, "message" => "Temporary folder removed."]);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user