Question

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

Was it helpful?

Solution

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);
}

OTHER TIPS

You can use the DirectoryIterator and unlink together.

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.

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);
}

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;
    }

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top