today I wand to answer the question how to delete a file with PHP.
First of all we have to separate. Files on the web server or files on the local hard disk. First yes, second no.
But on the web server is it – partly – possible to delete files. Partly means that you can’t delete every file. Also this would be a disaster.
The PHP-Script needs the right to delete files or at least specific files. Under Linux is the right system very strict.
But in the normal situation is it not so complicated like it sounds like. Because usually you delete a file which you created with the script. Like a temporary file or a file from a user upload (a picture for example). Such a file was created by the PHP script and inherit it’s user rights. And also the script has the right to delete this file again.
Very simple. All we need is the PHP command for deleting a file and the path of the file itself. It fits all in one code line.
$res = unlink("testdatei.hm");
That’s it. In $res we get a return value. 1 = successful deleted or 0 = Couldn’t delete the file.
You can use relative as well as absolute paths.
That’s it. Hope it was helpful :)