2
rmdir("./uploads/temp/".$user."/");

I have many files in a directory I wish to remove in my PHP script, however these is no way for me to unlink() the file first. Is there a way I co do

unlink(* FROM (dir=)) // don't downvote the long shot
// delete all files from the dir first
// then delete that dir 

Reference a directory has to be empty in order to delete it, see php.net/manual/en/function.rmdir.php

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

6 Answers6

3

You can use the DirectoryIterator and unlink together.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
3

use this

function delete_directory($dirname) {
   if (is_dir($dirname))
      $dir_handle = opendir($dirname);
   if (!$dir_handle)
      return false;
   while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
         else
            delete_directory($dirname.'/'.$file);    
      }
   }
   closedir($dir_handle);
   rmdir($dirname);
   return true;
}

This code can easily be improved upon, as it's a quick hack, but it takes a directory as an argument and then uses functional recursion to delete all files and folders within, and then finally removes the directory. Nice and quick, too.

Adeel Mughal
  • 776
  • 3
  • 12
2

There is no other way except to delete all files first using one way or another and then remove directory.

public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
    throw new InvalidArgumentException('$dirPath must be a directory');
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
    $dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
    if (is_dir($file)) {
        self::deleteDir($file);
    } else {
        unlink($file);
    }
}
rmdir($dirPath);
}
Shawn Taylor
  • 3,974
  • 4
  • 17
  • 23
1

Try using glob to loop over the files in the directory to delete

foreach (glob('/path/to/directory/*') as $file){
    unlink('/path/to/directory/' . $file);
}
fin1te
  • 4,289
  • 1
  • 19
  • 16
1

Check this http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

function recursive_remove_directory($directory, $empty=FALSE)
    {
        if(substr($directory,-1) == '/')
        {
            $directory = substr($directory,0,-1);
        }
        if(!file_exists($directory) || !is_dir($directory))
        {
            return FALSE;
        }elseif(is_readable($directory))
        {
            $handle = opendir($directory);
            while (FALSE !== ($item = readdir($handle)))
            {
                if($item != '.' && $item != '..')
                {
                    $path = $directory.'/'.$item;
                    if(is_dir($path)) 
                    {
                        recursive_remove_directory($path);
                    }else{
                        unlink($path);
                    }
                }
            }
            closedir($handle);
            if($empty == FALSE)
            {
                if(!rmdir($directory))
                {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }
Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
0

You can delete it recursively:

public function delete_folder ($path) { 
    $handle = opendir($path); 
    while ($file = readdir($handle)) { 
        if ($file != '..' && $file != '.') { 
            if (is_file($path . DS . $file))
                unlink($path . DS . $file); 
            else 
                delete_folder($path . DS . $file);
        } 
    } 
    closedir($handle); 
    rmdir($tmp_path); 
} 

delete_folder('path/to/folder');
gintas
  • 2,118
  • 1
  • 18
  • 28