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 <aidan@php.net>
* @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 <aidan@php.net>
* @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
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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Ok, thanks...

 

 

Now it isn't copying my files...

 

Why would that be?

 

EDIT:

WTF? It isn't working again! It is returning false! Something must be wrong with my host. It sure isn't my code that is randomly failing...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.