Deleting files in PHP is simple as long as you use the Unlink function to complete the deletion, if you want to delete all the files in the directory we need to use the recursive operation directory to delete.
Remember the lesson created from PHP files, we created a file named TestFile.txt.
The code is as follows |
Copy Code |
$myFile = "TestFile.txt"; $fh = fopen ($myFile, ' w ') or Die ("can ' t open file"); Fclose ($FH); Determine if it was deleted. $myFile = "TestFile.txt"; Unlink ($myFile); |
Cases
The code is as follows |
Copy Code |
$filename = ' file.txt '; fopen ($filename, ' A + '); if (!unlink ($filename)) { echo "File {$filename} delete failed"; } Else { echo "File {$filename} deleted successfully"; } ?> |
Delete all files under directory
The code is as follows |
Copy Code |
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 "successfully deleted file: $dirName/$item n "; } } } Closedir ($handle); } } |
http://www.bkjia.com/PHPjc/633098.html www.bkjia.com true http://www.bkjia.com/PHPjc/633098.html techarticle deleting files in PHP is simple as long as you use the Unlink function to complete the deletion, if you want to delete all the files in the directory we need to use the recursive operation directory to delete. Please remember from PHP files ...