在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 ); } } |
http://www.bkjia.com/PHPjc/633098.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633098.htmlTechArticle在php中刪除檔案很簡單只要使用unlink函數即可完成刪除,如果要刪除目錄下所有檔案我們需要利用遞迴操作目錄進行刪除。 請記住從PHP檔案...