Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: (solved) delete dir and content

  1. #1
    Join Date
    Feb 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question (solved) delete dir and content

    how do i get this to delete subfolders and its files aswell
    Code:
    $mydir = $path.$file;
    $d = dir($mydir);
    while($entry = $d->read()) {
     if ($entry!= "." && $entry!= "..") {
     unlink($mydir."/".$entry);
    
     }  
    }
    $d->close();
    rmdir($mydir);
    Last edited by dsjoes; 03-20-2011 at 10:13 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    DJR33 wrote one at the end of this thread. http://www.dynamicdrive.com/forums/s...ad.php?t=61310
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Feb 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i have tryed the below but it just refreshes the page nothing happens
    Code:
    <?php
    $path = "../../gallery2.0/gallery_files/gallery/";
    if(isset($_POST['file']) && is_array($_POST['file']))
    {
    	foreach($_POST['file'] as $file)
    	{	
    		
    $dirname = $path.$file; 
    
    //Recursive Delete Function
    
    function DELETE_RECURSIVE_DIRS($dirname) 
    { // recursive function to delete 
      // all subdirectories and contents: 
      if(is_dir($dirname))$dir_handle=opendir($dirname); 
      while($file=readdir($dir_handle)) 
      { 
        if($file!="." && $file!="..") 
        { 
          if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); 
          else DELETE_RECURSIVE_DIRS($dirname."/".$file); 
        } 
      } 
      closedir($dir_handle); 
      rmdir($dirname); 
      return true; 
    }
    
    	}
    }
    ?>
    i code is from here
    http://www.php.happycodings.com/File...on/code44.html

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    My code might work (in the link from Bluewalrus) but I didn't test it and it has no error correction. There are some more versatile functions here:
    http://php.net/manual/en/function.rmdir.php

    One of those should definitely work.

    It sounds to me like you might have file permissions problems. Are you on a linux server? Do you know how to use chmod to change the permissions for 777 or 755? (Note: you must do this for this directory and all directories/files inside it. Most FTP programs have an option for that.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Feb 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i have tryed yours and it is still the same my folders are set at 755 i have checked on my host that they are at 755

    i used this code i wrote to create them
    Code:
    $file = $_GET['folder'];
    mkdir("../../gallery2.0/gallery_files/gallery/$file", 0755);
    echo "<br /><br /><center><h1>Folder created</h1></center>";
    i can delete the folder and images in the first folder with the script in my first post it is just the since i have added a folder inside called thumbnails that i can't delete it.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Are you sure that the thumbnails folder is 755 too (and all the files within it)? If it's working otherwise, that's the obvious answer. I'm not sure what else would be wrong.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Feb 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yes the folder is 755 but it is empty at the minute because i haven't finished the thumbnail creation script yet

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Does it help if you set it to 777?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Feb 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    still the same

    if i do this to the code in my first post it works because the thumbnail folder is empty but i get error messages because it trys to unlink the folder first and it then trys to rmdir the pictures.

    Code:
    <?php
    $path = "../../gallery2.0/gallery_files/gallery/";
    if(isset($_POST['file']) && is_array($_POST['file']))
    {
    	foreach($_POST['file'] as $file)
    	{	
    		
    $mydir = $path.$file;
    $d = dir($mydir);
    while($entry = $d->read()) {
     if ($entry!= "." && $entry!= "..") {
     unlink($mydir."/".$entry);
    rmdir($mydir."/".$entry); // i added this bit
     }  
    }
    $d->close();
    rmdir($mydir);
    
    	}
    }
    ?>
    Warning: unlink(../../gallery2.0/gallery_files/gallery/untitled/thumbnails) [function.unlink]: Is a directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 60

    Warning: rmdir(../../gallery2.0/gallery_files/gallery/untitled/test.jpg) [function.rmdir]: No such file or directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 61
    Last edited by dsjoes; 03-16-2011 at 04:04 PM.

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
  •