2009-09-01 113 views

回答

1

http://us.php.net/manual/en/function.unlink.php。 你會發現在評論中許多功能,包括做什麼,你需要

一個例子:

function unlinkRecursive($dir, $deleteRootToo) 
{ 
    if(!$dh = @opendir($dir)) 
    { 
     return; 
    } 
    while (false !== ($obj = readdir($dh))) 
    { 
     if($obj == '.' || $obj == '..') 
     { 
      continue; 
     } 

     if ([email protected]($dir . '/' . $obj)) 
     { 
      unlinkRecursive($dir.'/'.$obj, true); 
     } 
    } 

    closedir($dh); 

    if ($deleteRootToo) 
    { 
     @rmdir($dir); 
    } 

    return; 
} 
3
$dir = '/some/path/to/delete/';//note the trailing slashes 

$dh = opendir($dir); 
while($file = readdir($dh)) 
{ 
    if(!is_dir($file)) 
    { 
     @unlink($dir.$file); 
    } 
} 
closedir($dh); 
0

此功能將遞歸刪除(如rm -r)。小心!

function rm_recursive($filepath) 
{ 
    if (is_dir($filepath) && !is_link($filepath)) 
    { 
     if ($dh = opendir($filepath)) 
     { 
      while (($sf = readdir($dh)) !== false) 
      { 
       if ($sf == '.' || $sf == '..') 
       { 
        continue; 
       } 
       if (!rm_recursive($filepath.'/'.$sf)) 
       { 
        throw new Exception($filepath.'/'.$sf.' could not be deleted.'); 
       } 
      } 
      closedir($dh); 
     } 
     return rmdir($filepath); 
    } 
    return unlink($filepath); 
} 
2
function Delete($path) 
{ 
    if (is_dir($path) === true) 
    { 
     $files = array_diff(scandir($path), array('.', '..')); 

     foreach ($files as $file) 
     { 
       Delete(realpath($path) . '/' . $file); 
     } 

     return rmdir($path); 
    } 

    else if (is_file($path) === true) 
    { 
     return unlink($path); 
    } 

    return false; 
} 
+0

這也將刪除指定目錄 – apelliciari 2009-12-17 09:15:08

相關問題