Hi,
You can use unlink
to remove a file, and __FILE__
to get the full path to the current file :
unlink(__FILE__);
As a "proof" :
squale@shark:~/developpement/tests/temp
$ ll | grep 'remove-myself.php'
-rw-r--r-- 1 squale squale 25 2009-08-01 17:01 remove-myself.php
=> The file exists
squale@shark:~/developpement/tests/temp
$ cat remove-myself.php
<?php
unlink(__FILE__);
=> It contains the code I gave
squale@shark:~/developpement/tests/temp
$ php ./remove-myself.php
=> I launch the script
squale@shark:~/developpement/tests/temp
$ ll | grep 'remove-myself.php'
=> It doesn't exist anymore
For this to work, you'll have to be sure you have the required privilegies... this means the user trying to delete the file needs to have right-access on the directory containing it.
When you are in command line, it's generally OK ; but if you are trying to do this via Apache, you will need to give Apache write-access to that directory/file -- Apache doesn't generally have that kind of privilege by default (not secure, and generally not needed)
Not sure it'd be possible on windows, though... It works on Linux, but Windows might kinda "lock" the file when it's being executed...