Solved

Problems with directory removing.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to count occurrences of each item in an array.

743 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