Jump to content

Loop Deleting folder, not contents


envexlabs

Recommended Posts

Hey,

 

I found this loop on the internet, but it's seems to be deleting the whole folder, not the contents.

 

any insight?

 

<?php
/**
* Delete a file, or a folder and its contents
*
* @author Aidan Lister <[email protected]>
* @version 1.0.2
* @param string $dirname Directory to delete
* @return bool Returns TRUE on success, FALSE on failure
*/

$tmpdir = $_SERVER['DOCUMENT_ROOT'].'/uploads/tmp/';

function rmdirr($dirname)
{
// Sanity check
if (!file_exists($dirname)) {
return false;
}

// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}

// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}

// Recurse
rmdirr("$dirname/$entry");
}

// Clean up
$dir->close();
return rmdir($dirname);
}

rmdirr($tmpdir);

?>

Link to comment
https://forums.phpfreaks.com/topic/123500-loop-deleting-folder-not-contents/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.