-
Deleting files with PHP?
0
June 2nd, 1970UncategorizedFollowing our tutorials on uploading files and listing files, this tutorial will walk you through deleting files from a directory. This is done using the unlink() function.
To start off, we create a function and pull through the file we’re going to delete.
function file_delete($file) {
}
Then, we need to make sure that the file we’re trying to delete isn’t currently open. Confusing, we also have to make sure it’s open before we do this!
function file_delete($file) {
$ofile = fopen($file, ‘w’) or die(”error opening file”);
fclose($ofile)}
The last part of this function is to delete the file. Easy no?
function file_delete($file) {
$ofile = fopen($file, ‘w’) or die(”error opening file”);
fclose($ofile)unlink($file)
}Finally, we call the function from the page, passing through the file path for the file we want to delete.
file_delete(”images/intro.jpg”);
