PHP Delete File
Syntax
unlink($file);
Example
$file = "sample.txt";
if (!unlink($file)) {
echo ("Error deleting $file");
} else {
echo ("Deleted $file");
}
Output
Deleted sample.txt
Explanation
The unlink()
function is used to delete a file in PHP. It accepts the location of the file as a parameter and returns TRUE
on success and FALSE
on failure.
In the example above, the unlink()
function is used to delete a file named "sample.txt". The if-else
statement is used to check whether the file was successfully deleted or not. If the file is not deleted, an error message is printed to the console.
Use
The unlink()
function is primarily used to delete files in PHP.
Important Points
- The
unlink()
function can only delete files and not directories. - The function returns
TRUE
on success andFALSE
on failure.
Summary
To summarize, the unlink()
function is used to delete files in PHP. It accepts the location of the file as a parameter and returns TRUE
on success and FALSE
on failure. The function cannot be used to delete directories.