如何使用php unlink删除文件
生活随笔
收集整理的這篇文章主要介紹了
如何使用php unlink删除文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
php unlink刪除文件的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后使用unlink函數(shù)刪除文件,其語句為“if (unlink($file_delete)) {...}”;最后執(zhí)行該示例文件即可。
推薦:《PHP視頻教程》
使用php unlink刪除文件
php unlink()函數(shù)介紹
unlink — 刪除文件
語法:
bool unlink ( string $filename [, resource $context ] )
刪除 filename。和 Unix C 的 unlink() 函數(shù)相似。 發(fā)生錯(cuò)誤時(shí)會(huì)產(chǎn)生一個(gè) E_WARNING 級(jí)別的錯(cuò)誤。
參數(shù):
filename:文件的路徑。
context:在 PHP 5.0.0 中增加了對上下文(Context)的支持。有關(guān)上下文(Context)的說明參見 Streams。
返回值:
成功時(shí)返回 TRUE, 或者在失敗時(shí)返回 FALSE。
php unlink()實(shí)例:
php使用unlink()刪除一個(gè)文件
<?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";
}
?>
php使用遞歸的方法刪除目錄中的所有文件:
<?php
function delDir($directory){//自定義函數(shù)遞歸的函數(shù)整個(gè)目錄
if(file_exists($directory)){//判斷目錄是否存在,如果不存在rmdir()函數(shù)會(huì)出錯(cuò)
if($dir_handle=@opendir($directory)){//打開目錄返回目錄資源,并判斷是否成功
while($filename=readdir($dir_handle)){//遍歷目錄,讀出目錄中的文件或文件夾
if($filename!='.' && $filename!='..'){//一定要排除兩個(gè)特殊的目錄
$subFile=$directory."/".$filename;//將目錄下的文件與當(dāng)前目錄相連
if(is_dir($subFile)){//如果是目錄條件則成了
delDir($subFile);//遞歸調(diào)用自己刪除子目錄
}
/* http://www.manongjc.com/article/1351.html */
if(is_file($subFile)){//如果是文件條件則成立
unlink($subFile);//直接刪除這個(gè)文件
}
}
}
closedir($dir_handle);//關(guān)閉目錄資源
rmdir($directory);//刪除空目錄
}
}
}
delDir("mydir");//調(diào)用delDir函數(shù)
?>
總結(jié)
以上是生活随笔為你收集整理的如何使用php unlink删除文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【池州seo】为什么企业网站在搜索引擎中
- 下一篇: 鲜龙虾怎么做 鲜龙虾的做法