pugboy Posted June 7, 2008 Share Posted June 7, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/ Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 chmod permissions for the folder/files set right? Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-559942 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Everything is 777... It shows a blank page, so I assume there is some kind of error. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-559943 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 well i mean, you're executing server commands to delete a file. If you want some sort of output, you have to echo something. Go on your server and see if the stuff is still there... Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-559953 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 I have HTML after the PHP code, so something is stopping the rest of the file from being displayed... Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-559958 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 Well did you go to your server and check if the files/folders are still on your server or are they being properly deleted? If they are, then at least we know that part is working... Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-559972 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Nope. Everything seems to not work :/ I ran it through a syntax checker online, and it said this line (below) had an extra period: if ($entry == ‘.’ || $entry == ‘..’) { Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560010 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 try using regular single quote ' ' instead of those..whatever those are. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560042 Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Yeah, don't use those smart quote things. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560045 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560053 Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Try the realpath() function if you can't get it to work like that, but it should. Check permissions. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560066 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Hmm... Works with realpath... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560112 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Sorry for the double post... Never mind... Realpath is outputting a blank string, so I get an error. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560119 Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Can I see what code you're using? Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560121 Share on other sites More sharing options...
pugboy Posted June 8, 2008 Author Share Posted June 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560130 Share on other sites More sharing options...
DarkWater Posted June 8, 2008 Share Posted June 8, 2008 Heh. I see the problem. $dir = "/../account/" . $user . "/" . $type; Should be: $dir = "../account/" . $user . "/" . $type; That / in the beginning is referring to the root directory of the filesystem. Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560131 Share on other sites More sharing options...
pugboy Posted June 8, 2008 Author Share Posted June 8, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/109154-delete-folder-and-files/#findComment-560132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.