Skip to main content

Unlink File in Codeigniter

You can delete files from the server with PHP’s file system function unlink() which will delete or remove the files. It it remove all type of files like images,document, media files .. etc.It will returns TRUE on success or FALSE on failure.If there is a problem, it is most likely because you do not have permission to delete the file.The directory must be writable by the user in order to remove the file.File not required write permissions.

i am gonna use is_readable() function which tells whether a file exists and is readable or not, It is better to check before attempting to delete action.

For example if you want to delete files from upload folder(which is located at web server root folder).


<?php

$file = "uploads/my_test_file.txt";

if (is_readable($file) && unlink($file)) {
echo "The file has been deleted";
} else {
echo "The file was not found or not readable and could not be deleted";
}


That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.