Home > php教程 > php手册 > php文件删除unlink()详解

php文件删除unlink()详解

WBOY
Release: 2016-06-13 09:47:47
Original
864 people have browsed it

在php中删除文件很简单只要使用unlink函数即可完成删除,如果要删除目录下所有文件我们需要利用递归操作目录进行删除。

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

 代码如下 复制代码

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
判断是否删除了.

$myFile = "testFile.txt";
unlink($myFile);

 代码如下 复制代码

$filename = 'file.txt';
fopen($filename,'a+');
if(!unlink($filename))
{
echo "文件{$filename}删除失败";
}
else
{
echo "文件{$filename}删除成功";
}
?>

删除目录下所有文件

 代码如下 复制代码

function delFileUnderDir( $dirName="../Smarty/templates/templates_c" )
{
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
n";
   }
   }
   }
   closedir( $handle );
}
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template