php unlink, unlink php, delete file php, unlink, unset, php unlink file, delete a file in php, php unlink permission denied, unlink php,

How to delete or unlink a file in PHP

Deleting features in any system is a must have since it allows removing any data that is no longer required from the system. Systems that have a delete feature always face the problem of deleting files when removing data.

When deleting of files is not handled when deleting data, it may result in consumption of storage space assigned to the system in the server, therefore resulting in a slower system and unnecessary space usage which limits new data from being added.

In previous articles, we discussed how you can upload image files using PHP and jQuery and also how you can upload multiple files using PHP and jQuery.

For this article, we shall discuss how to delete a file or image in PHP or how to unlink a file in PHP during delete. The deleting or unlinking of a file from the server removes the file from the folder where it is stored ensuring that it will not use any space from the server.

In PHP, deleting from a database is done by the use of the DELETE keyword which one has to specify the table which the data is being deleted from and also specify the row which will be deleted else all the data from a given table will be deleted.

For example, deleting all records from a table named users, use the below code

<?php

mysqli_query($con, "DELETE FROM users");

?>

To delete a specific record from a table called users, use the below code

<?php

mysqli_query($con, "DELETE FROM users WHERE id='1'");

?>

How to delete files using PHP

Unlike MySQL query for delete that uses the DELETE function, deleting files using PHP uses unlink() function.

Unlink function is different from unset() function. The unset() function in PHP is used to make a file empty without deleting the file itself, that is, it only removes what is contained in a defined file.

The unset() function mostly returns an empty result since the contents in the file or variable will be removed from it.

How to use unlink() function in PHP

The unlink() function in PHP is an inbuilt function. An inbuilt function in PHP as we discussed in a previous article, we highlighted that it’s a pre-defined function in PHP language that is designed to perform a particular task.

The unlink() function deletes a file completely from its location in the server and therefore creates a space that can be occupied by another file.

The unlink() function in PHP takes one mandatory parameter that contains the file name that will be deleted.

Note that, if the file is in a different location from where the unlink() function is being executed, you should add the absolute or relative location to the file in the same mandatory parameter.

The unlink() function format is as shown below

<?php

// delete file using unlink function

unlink($filename);

?>

Example 1

For example, if we want to delete an image with a name image1.png that is in the same location as the unlink file, we use the code below

<?php

// file to delete

$filename="image1.png";

// delete file using unlink function

unlink($filename);

?>

Example 2

Deleting a pdf file with name document1.pdf that is not in the same location as the unlink file, we use the code below

<?php

// file to delete on a different location

$filename="../documents/document1.pdf";

// delete file using unlink function

unlink($filename);

?>

Conclusion

From the above discussion, we have shown how to delete a file or image using the unlink function in PHP and also we have differentiated unlink and unset inbuilt functions.

One thing you should check in case the unlink function fails to work is that, check the file permissions that the files have access, read and write permissions.