Solved

Problems with directory removing.

Posted on 2004-04-05
4
418 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now