Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: unlink()?

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Regina, Saskatchewan, Canada
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    unlink()?

    How do you delete a directory?

    I have always used unlink() to delete files, and just the other day tried to delete a directory with it. This is what I put:

    unlink("./folder/");

    Is there a reason that doesnt work? Do I need an extra parameter if it is for a directory?

    Thanks in advance!
    Jared Brandt
    IKinsler

  • #2
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure the dir dosn't have any files in it? If so it won't work : you need to delete them first.
    I don't suffer from insanity, I enjoy every single minute of it!

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Regina, Saskatchewan, Canada
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OH! Well that's my problem then! I've still got files! I will try deleting them and THEN rmdir().

    Thanks to both of you!
    Jared Brandt
    IKinsler

  • #5
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a function somebody came up with a while ago that will clean the files in the dir and them remove the dir...

    PHP Code:
    <?php
    function delete($file) {
     
    chmod($file,0777);
     if (
    is_dir($file)) {
      
    $handle opendir($file); 
      while(
    $filename readdir($handle)) {
       if (
    $filename != "." && $filename != "..")
    {
        
    delete($file."/".$filename);
       }
      }
      
    closedir($handle);
      
    rmdir($file);
     } else {
      
    unlink($file);
     }
    }
    delete("ordnername");
    ?>
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    Regina, Saskatchewan, Canada
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Feyd!

    I had that figured out, but unfortunately that doesn't work either! You see, I have no idea how many directories are in the directory I wish to delete - it could be 10 or 20! I had a function that would delete it if it was a file, and empty it and then delete it if it was a dir. But, apparently you cannot launch a function if you're already in it! So that didn't work either.
    Jared Brandt
    IKinsler

  • #7
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about having two functions :

    One that does the cleaning
    One that calls the one that does the cleaning.

    The the first function goes upon another dir it calls the second function which calls the first.....

    Should work.....
    I don't suffer from insanity, I enjoy every single minute of it!

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    not for the feint-hearted...

    exec('rm -f -R /get/this/path/right/directory');

    but be sure to get the path right else tears will be forthcoming & dont blame me.
    note some hosts disallow exec() calls.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •