SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How do I unlink this?

    I have a very simple question, I guess.

    I need to remove a folder and all it's contents. Not sure if I need to change the permissions as well.
    This is what I have right now, that isn't working.

    PHP Code:
    $file "../images/".$code;
    unlink($file); 
    The name of the folder that is going to be deleted is in $code.
    Should this code work? Do I only need to change the chmod?
    If so, how can I do this prior to the delete code?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thank you. That one worked for me with an empty folder.
    But since I have things in the folder as well I tried to add something, but it won't delete my files. Why not?

    PHP Code:
    unlink("$file/*.*");
    rmdir($file); # deletes the folder 
    Then I tried to add one code that deletes all file types I think will be in the folder with this code.
    But that one isn't working either.

    PHP Code:
    unlink("$file/*.mp3");
    unlink("$file/*.wav");
    unlink("$file/*.aif");
    unlink("$file/*.jpg");
    unlink("$file/*.png");
    unlink("$file/*.gif");
    rmdir($file); # deletes the folder 
    What is wrong with my code?
    When using just the rmdir to remove an epmty folder it works fine, but I have to remove things in the folder first...

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I solved it finally.

    I found something that worked.
    The one called $file is for the folder. Then I made one called $file2 for the filenames inside the folder.

    PHP Code:
    $file "../images/".$code;
    $file2 $file.'/*';
    foreach (
    glob("$file2.*") as $filename) {
       
    unlink($filename);
    }
    rmdir($file); # deletes the folder 


Bookmarks

Posting Permissions

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