Previous Next  

PHP unlink() Function

The unlink() function in PHP is used to delete a file from the file system. This function removes the specified file and is commonly used when you want to delete files, such as temporary files or uploaded files, from your server.

unlink(string $filename)

Where, $filename (required) is path to the file you want to delete. It can be either an absolute path or a relative path.

The function returns true if the file is successfully deleted. The function returns false if the file cannot be deleted, for example, due to permission issues or if the file does not exist.

Example: Deleting a File

In this example, we'll delete a file named example.txt in the current directory.

<?php

$filename = 'example.txt';


if (file_exists($filename)) {

if (unlink($filename)) {

echo "The file $filename has been deleted.";

} else {

echo "Error: Unable to delete the file.";

}

} else {

echo "The file $filename does not exist.";

}

?>

  Previous Next  

Aspirant's Home is an innovative educational and career development platform designed to empower individuals in achieving their academic and professional aspirations.
Terms of Use   About Us   Privacy Policy