Solved

Problems with directory removing.

Posted on 2004-04-05
4
420 Views
Last Modified: 2013-12-13
Hi there,

I'm currently working on picture gallery administration tool, and I use file management functions, to upload/delete files; create and remove directories. Everything was fine until i had to make directory removing script. Of course, first of all i have to erase every file inside it:

<?
      if ($handle = opendir($path)) {
        while (false !== ($file = readdir($handle))) {
          unlink ("$path/$file");
        }

?>

After it's done I'd like to remove the directory these files were in, just simply like that:

<?
rmdir($path);
?>

BUT, however even if there is no files in the directory, there is still something left. And that something is two elements: "." and ".." (no quotes).

".." element should mean something like "Up one level in the dir" or something like that, don't know anything about "." ...

However, the file deleting loop written above can't delete those double-and-single-dot elements and i get Permission denied error.

(Notice that all other files are deleted by the loop, but those two ".." & "." are left, so visually the directory is empty, but the server doesn't accept it as the empty one because of those dots).

Furthermore i can't remove the directory because it's not empty and those dots are left which i can't delete.


OK, here are the errors that i get for you to imagine:

Warning: unlink(C:/Apache/htdocs/spoilera/nuotraukos/thumbs/testdir/.): Permission denied in c:\apache\htdocs\spoilera\admin\includes\fc.php on line 106

Warning: unlink(C:/Apache/htdocs/spoilera/nuotraukos/thumbs/testdir/..): Permission denied in c:\apache\htdocs\spoilera\admin\includes\fc.php on line 106

Warning: rmdir(): Directory not empty in c:\apache\htdocs\spoilera\admin\includes\fc.php on line 120

----

see? those dots don't let me remove the directory.

Every idea would be appreciated.
0
Comment
Question by:Arturas86
4 Comments
 
LVL 2

Accepted Solution

by:
Fataqui earned 50 total points
ID: 10756593
Hi


You can not remove the directory because you are still in it! use closedir (); on the $handle after your loop and before your rmdir();

Example ( as a function [ remove directories and files! ] ).....

<?

// remove all files and directories below this directory

$path = "e:/www/docs/test";

function remove ( $dir )
{
      $keep[]  = $dir;
      $remove  = array ( $dir );
      $removed = array ();

      while ( count ( $remove ) > 0 )
      {

            foreach ( $remove as $directory => $start )
            {

                  if ( $dir = @opendir ( $start ) )
                  {

                        while ( ( $file = readdir ( $dir ) ) !== false )
                        {

                              if ( ( $file != '.' ) && ( $file != '..' ) )
                              {

                                    $real = $start . '/' . $file;


                                    if ( is_dir ( $real ) )
                                    {


                                          if ( !in_array ( $real, $removed ) && !in_array ( $real, $remove ) )
                                          {

                                                $remove[] = $real;
                                                array_unshift ( $keep, $real );

                                          }

                                    } elseif ( is_file ( $real ) )
                                    {

                                          $files[] = $real;

                                    }

                              }

                        }

                        closedir($dir);

                  }

                  $removed[] = $start;

                  unset ( $remove[$directory] );

                  }

            }


            foreach ( $files as $name )
            {

                  unlink ( $name );

            }

            foreach ( $keep as $rmd )
            {

                  rmdir ( $rmd );

            }

      }

// run it

remove ( $path );

?>


F!
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 10808331
A little precision. You dont have to delete . and .. these are not real files or directories.
0
 
LVL 9

Expert Comment

by:techtonik
ID: 10809806
These special names "." and ".." are used by the system to designate current working directory and parent directory respectively. So you can't delete a directory while you're working in it and of course can't delete parent. If there is only "." and ".." names - that means, that your current working directory is already empty and you can delete it from parent dir - go up one level by chdir("..") and call rmdir("testdir");
0
 

Author Comment

by:Arturas86
ID: 10811848
Thank you for your comments, guys. I think I found a solution.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question