rmdir() Not Deleting an Empty Folder - PHP
<p>I'm currently building a very low level CMS for friend's artist web page that will allow her to upload, edit, and delete images along with designating categories for them and posting news posts about shows and so on.</p>

<p>I'm sure there is a very easy solution to this problem of mine but my inexperience in programming has me left at a loss; so here goes.</p>

<p><strong>The Problem</strong></p>

<p>The problem occurs on a page where the user can delete an image that has been uploaded. Here is the snippet of code where the problem occurs:</p>

<pre><code>    // Assign selection to variables in memory...
    $img_id = $data["img_name"];

    // First, collect the file path to the image being deleted...
    $rs = mysql_query("SELECT img_path FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());
    list($img_path) = mysql_fetch_row($rs);

    // Then delete that row from the DB...
    mysql_query("DELETE FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());

    // Now, using the file path collected earlier, delete that file from the server.
    unlink($img_path);

    // Quickly make sure that the file has been deleted by checking if it exists... if it still exists return error.
    if(file_exists($filename)) {
        $err[] = "ERROR - There was an error deleting the file! Please try again.";
        $_SESSION["errors"] = $err;
        header("Location: img_del.php?doDel=failed");
        exit();
    }

    // Scan the directory now that a file has been deleted to see if the dir is empty. If so, delete it. (No use in having empty folders!)
    $file_types = array("gif","jpg","png"); // file types to scan for...
    $path_parts = pathinfo($img_path); // get the directory from the file path...
    $dir = $path_parts["dirname"] . "/"; // assign it to a new variable...
    $handle = opendir($dir);
    $scan = scandir($dir); // now, scan that directory...
    $image_found = FALSE;
    for($i=0; $i&lt;count($scan); $i++) { 
        if ($scan[$i] != '.' &amp;&amp; $scan[$i] != '..' &amp;&amp; in_array(end(explode('.', $scan[$i])), $file_types)) { 
            $image_found = TRUE;
        }
    }
    closedir($handle);
    if(!$image_found) {
        rmdir($dir);
    }
</code></pre>

<p>I first delete the DB row containing image info, then delete the file from the server. <em>this works fine</em>, however, I also want to check if the directory is left empty after deleting that file. I check if the directory is empty using a loop and if no file is found, I run <code>mkdir()</code>. <strong>For some reason it keeps returning an error saying that the directory is not empty</strong></p>

<p>I've searched the web and this site for a solution but I've yet to find one. I'm sure it's out there but I'm having trouble finding it which why I came here. What should I do?</p>

<p>Thanks in advance for any help submitted!</p>

<p><strong>NOTE</strong>
I have also checked for hidden files and folders but no luck...</p>

<p><a href="http://img197.imageshack.us/img197/4848/unled1goy.png" rel="nofollow">Here is a link to an image that pretty much sums up my problem in a nutshell</a></p>
