Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problems with directory removing.

Posted on 2004-04-05
4
Medium Priority
?
425 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 200 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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