开发者

How can I delete the contents of a folder with PHP?

开发者 https://www.devze.com 2023-01-21 17:05 出处:网络
I can\'t seem to get this function to delete all my files and folders in a specific directory can someone help me fix this problem?

I can't seem to get this function to delete all my files and folders in a specific directory can someone help me fix this problem?

Here is my code.

$apps = 9;
$dirname = './members/' . $apps . '/';
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;
}


Your function looks fine.

I guess you are not calling it correctly. One way to call it is:

$apps = 9;
$dirname = './members/' . $apps . '/';
delete_directory($dirname);

function delete_directory($dirname) {
.....


function EmptyDir($dir) 
{
    $handle=opendir($dir);
    while (($file = readdir($handle))!==false) {
        unlink($dir.'/'.$file);
    }
    closedir($handle);
}

EmptyDir('yourdir'); 


Should the initial directory have a trailing slash?

$dirname = './members/' . $apps;
0

精彩评论

暂无评论...
验证码 换一张
取 消