php文件删除unlink()详解

请记住从PHP文件创建的教训,我们创建了一个文件,名为testFile.txt 。

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
判断是否删除了. http://www.manongjc.com/article/1351.html
$myFile = "testFile.txt";
unlink($myFile);

$filename = 'file.txt';
fopen($filename,'a+');
if(!unlink($filename))
{
echo "文件{$filename}删除失败"; //  http://www.manongjc.com/article/1351.html
}
else
{
echo "文件{$filename}删除成功";
}
?>

删除目录下所有文件

function delFileUnderDir( $dirName="../Smarty/templates/templates_c" )
{
// http://www.manongjc.com/article/1351.html
if ( $handle = opendir( "$dirName" ) ) {
   while ( false !== ( $item = readdir( $handle ) ) ) {
   if ( $item != "." && $item != ".." ) {
   if ( is_dir( "$dirName/$item" ) ) {
         delFileUnderDir( "$dirName/$item" );
   } else {
   if( unlink( "$dirName/$item" ) )echo "成功删除文件: $dirName/$item<br />n";
   }
   }
   }
   closedir( $handle );
}
}
原文地址:https://www.cnblogs.com/myhomepages/p/5992621.html