在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<br />n"; } } } closedir( $handle ); } } |
本文地址:http://www.phprm.com/base/44453.html
转载随意,但请附上文章地址:-)