PHP and MySQL (49 Blogs) Become a Certified Professional

How To Best Utilize Unlink Function In PHP?

Published on Sep 18,2019 7 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

This article will introduce you to a simple yet important concept that is PHP Unlink function with a detailed practical demonstration. Following pointers will be covered in this article,

PHP Unlink Function

In PHP, there are two functions that are identical in performing some undo operations but both of them differ in the type of data on which they are applied to perform such undo functionalities. they are unlink() and unset() where unlink acts as drop operation that will completely delete files wheres unset deletes the file content for emptying it. In this article, we are going to discuss about unlink() in php.

unlink()

It is an inbuilt function in PHP which is used to delete files. It is similar to UNIX unlink() function. The filename is sent as a parameter that needs to be deleted and the function returns True on successful execution whereas it returns false on unsuccessful execution.

  • Syntax: unlink( filename, context )
  • filename: It specifies the name of the file that has to be deleted.
  • context: It is an optional parameter that specifies the context of the file handle that can be used to modify the nature of the stream.

Web server user must have write permissions to the directory for using the unlink() function. It generates an E_WARNING level error on failure. It returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.

Moving on with this article on Unlink Function In PHP

Sample Program

Now, lets go through the demonstration of an example to delete a file named as file.txt using unlike() function

<?php
$filepoint = "file.txt";
if (!unlink($filepoint))
{
echo ("$filepoint cannot be deleted due to an error");
}
else
{
echo ("$filepoint has been deleted");
}
?>&nbsp;

Lets go through an example, If we want delete files with mask,

<?php
$mask = "*.txt";//extension
array_map( "unlink", glob( $mask ) );
?>

Glob() returns an array of directories or file names matching a specified pattern. Each value of an array to a user-made function is sent by array_map() , and returns an array with new values, given by the user-made function.

With this we come to an end of this article on PHP Unlink function. I hope you have learned about basic difference between unlink() & unset(), and particularly about the inbuilt function unlink() in PHP with some of the examples.

If you found this PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this article and I will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.