PHP does unlink function works with a path?

Masiar picture Masiar · Feb 15, 2011 · Viewed 25k times · Source

I would like to remove a file from a folder in PHP, but I just have the path to this file, would it be ok to give the path to unlink? For example

unlink('path/to/file.txt');

If this doesn't work, the only way to get rid of those files would be to create a .php file in the path/to/ directory and include it somehow in my file an call a method there to remove the file, right?

Answer

Nikz picture Nikz · Jul 13, 2013

Got an easy method for your question

Use this code to remove a file from a folder

$_SERVER['DOCUMENT_ROOT']

this can be used inside the unlink function

worked code

     unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt");