I have the following code in the efforts of checking if a directory is empty before using rmdir. I'm doing something wrong but don't know what. Can anyone help me figure this out? What's wrong with my code below, or is there a better way to check if a directory is empty other than readdir?

$OpenDir=opendir("../../Audio-MP3/Uploads/".$WebPage."/". stripslashes($Row["Directory"]));
		if (readdir($OpenDir)<>""){		
			if(is_dir("../../Audio-MP3/Uploads/".$WebPage."/". stripslashes($Row["Directory"]))){
				rmdir("../../Audio-MP3/Uploads/".$WebPage."/". stripslashes($Row["Directory"]));
			}
		}

    It looks like you got your order of checking wrong. It probably should be something like:

    $dir_path = "../../Audio-MP3/Uploads/" . $WebPage . "/" . stripslashes($Row["Directory"]);
    if (is_dir($dir_path) && ($dir_handle = opendir($dir_path))) {
        if (readdir($dir_handle) === false) {
            rmdir($dir_path);
        }
    }

      Nice, forgot a close parenthesis (just pointing that out for anyone else who may need this in their code) but it works awesome! Thanks so much!

        forgot a close parenthesis

        Good point, I edited the code just in case.

          Write a Reply...