jamkelvl Posted January 24, 2010 Share Posted January 24, 2010 This bit of code is not working, anyone know why? <?php $directory = "/../".$_GET['delete']; rmdir($directory); ?> Just trying to delete a folder (and all its contents) that is back one directory in the tree. Any help would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/189662-rmdir-or-unlink-not-working/ Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 Assuming that you have write permission to the directory concerned, then it still will not work unless the directory is empty. To delete a directory with files in it, you must first delete the files. Quote Link to comment https://forums.phpfreaks.com/topic/189662-rmdir-or-unlink-not-working/#findComment-1000976 Share on other sites More sharing options...
jamkelvl Posted January 24, 2010 Author Share Posted January 24, 2010 I do have permission to delete/create. I shall make something that deletes the files and post if it works or not. Quote Link to comment https://forums.phpfreaks.com/topic/189662-rmdir-or-unlink-not-working/#findComment-1000980 Share on other sites More sharing options...
jamkelvl Posted January 25, 2010 Author Share Posted January 25, 2010 Found a nice bit of code... <?php function deleteDir($dir) { // open the directory $dhandle = opendir($dir); if ($dhandle) { // loop through it while (false !== ($fname = readdir($dhandle))) { // if the element is a directory, and // does not start with a '.' or '..' // we call deleteDir function recursively // passing this element as a parameter if (is_dir( "{$dir}/{$fname}" )) { if (($fname != '.') && ($fname != '..')) { echo "<p class=success>Deleting Files in the Directory: {$dir}/{$fname}</p>"; deleteDir("$dir/$fname"); } // the element is a file, so we delete it } else { echo "<p class=success>Deleting File: {$dir}/{$fname} </p>"; unlink("{$dir}/{$fname}"); } } closedir($dhandle); } // now directory is empty, so we can use // the rmdir() function to delete it echo "<p class=success>Deleting Directory: {$dir} </p>"; rmdir($dir); } // call deleteDir function and pass to it // as a parameter a directory name deleteDir("../".$_GET['delete']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/189662-rmdir-or-unlink-not-working/#findComment-1001148 Share on other sites More sharing options...
salathe Posted January 25, 2010 Share Posted January 25, 2010 Found a nice bit of code... That could probably be written a lot simpler using a RecursiveDirectoryIterator, or are you happy with what's there? Quote Link to comment https://forums.phpfreaks.com/topic/189662-rmdir-or-unlink-not-working/#findComment-1001182 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.