php unlink删除文件的方法:首先创建一个PHP示例文件;然后使用unlink函数删除文件,其语句为“if (unlink($file_delete)) {...}”;最后执行该示例文件即可。

推荐:《PHP视频教程》
使用php unlink删除文件
php unlink()函数介绍
unlink — 删除文件
语法:
1 | bool unlink ( string $filename [, resource $context ] )
|
Salin selepas log masuk
删除 filename。和 Unix C 的 unlink() 函数相似。 发生错误时会产生一个 E_WARNING 级别的错误。
参数:
filename:文件的路径。
context:在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。
返回值:
成功时返回 TRUE, 或者在失败时返回 FALSE。
php unlink()实例:
php使用unlink()删除一个文件
1 2 3 4 5 6 7 8 9 | <?php
$file_delete = "home/meeta/my.php" ;
if (unlink( $file_delete )) {
echo "The file was deleted successfully." , "\n" ;
} else {
echo "The specified file could not be deleted. Please try again." , "\n" ;
}
?>
|
Salin selepas log masuk
php使用递归的方法删除目录中的所有文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php
function delDir( $directory ){
if ( file_exists ( $directory )){
if ( $dir_handle =@opendir( $directory )){
while ( $filename =readdir( $dir_handle )){
if ( $filename !='.' && $filename !='..'){
$subFile = $directory . "/" . $filename ;
if ( is_dir ( $subFile )){
delDir( $subFile );
}
if ( is_file ( $subFile )){
unlink( $subFile );
}
}
}
closedir ( $dir_handle );
rmdir ( $directory );
}
}
}
delDir( "mydir" );
?>
|
Salin selepas log masuk
Atas ialah kandungan terperinci 如何使用php unlink删除文件. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!