Solved

Problems with directory removing.

Posted on 2004-04-05
4
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.

696 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