Jump to content

Delete folder and files


pugboy

Recommended Posts

I have searched quite a bit, and can't find anything to delete a folder and all of the files. I treid the glob function to output the files in a array, but that didn't work.

 

I also tried the user comments on the documentation page and http://putraworks.wordpress.com/2006/02/27/php-delete-a-file-or-a-folder-and-its-contents/

 

 

Is there one that works? The link above just outputs a blank page when I run the script provided...

 

I tried this:

session_start();
$do = $_GET["do"];
$type = $_POST["type"];
$user = $_SESSION["user"];
if(!isset($_SESSION["user"])){
echo "LOGGED OUT";
} else {
/**
* 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
*/
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);
}
rmdir("[b]../[/b]account/" . $user . "/" . $type);
}
?>

 

Notice the periods signifying I want to delete a folder not in the current directory...

 

 

And I also tried it as a file in the root folder:

 

session_start();
$do = $_GET["do"];
$type = $_POST["type"];
$user = $_SESSION["user"];
if(!isset($_SESSION["user"])){
echo "LOGGED OUT";
} else {
/**
* 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
*/
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);
}
rmdir("account/" . $user . "/" . $type);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/
Share on other sites

Ok, thanks :)

 

 

Also, is it possible to create a directory like this:

 

account

-Admin

--[Directory i want to create]

usercp

-Add.php (The file creating the folder)

 

 

So the code:

 

$dir = "../account/Admin/Dir";

mkdir($dir);

 

It doesn't work when I do that. Do I really have to specify an ABSOLUTE path?

 

if($do=="add"){
echo "Checking files...<br/>";
$baseDir = "/../account/" . $user;
$dir = "/../account/" . $user . "/" . $type;
$dirTemplate = "/../template/" . $type;
$fileList = $dirTemplate . "/files.txt";
$passwords = md5($user);
$passwords = $passwords . ".txt";
$indexfile = $dirTemplate . "/index.html";
echo "Files Intact... Copying files... <br/>";
echo "Creating directory: $dir<br/>";
if(!mkdir($dir)){
	echo "Fatal Error. Directory did not create";
	exit;
} else {
	echo "Directory Created.<br/>";
}
$fileName = $fileList;
$fh = fopen($fileName, 'r');
$fileListB = fread($fh, filesize($fileName));
fclose($fh);
$fileListC = explode("\n",$fileListB);
echo "Template Exists... Copying Files...";
for ( $id = 0; $id <= $maxid; $id += 1) {
	$fileT = $dirTemplate . "/" . $fileListC[$id];
	$fileD = $dir . "/" . $fileListC[$id];
	if(!copy($fileT,$fileD)){
		echo "Failed to copy $fileT to $fileD";
		exit;
	} else {
		echo "Copy file: $fileT. Success!";
	}
}
}

 

I removed the realpath...

 

 

Also, the mkdir function just doesn't work for some reason... It always returns false, and no directory is created. I chmoded everything to 777.

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.