Learn Codeigniter

Delete File Using unlink function In PHP

To Delete file,we will use unlink() function of PHP.We need to pass complete path of file in unlink function.We can delete file only if we have required permission.

Syntax

unlink ($path)

Example

In below example we delete file from images folder.

<?Php
$folder=getcwd(); // get current working directory
$imagePath=$folder."/"."images/"."hello.jpg"; // Complete image path
if(file_exists($imagePath)){
chmod($imagePath, 0777); // Change permission of image 
if(unlink($imagePath))
{
echo "File has been deleted.";
} 
else
{
echo "Please Try Again!";
    }
  }
?>