Is it possible to create a PHP file that runs once with no errors and deletes itself?
Asked
Active
Viewed 1.4k times
28
-
4Out of interest, why do you wish to do this? – Mark Byers Apr 05 '10 at 14:58
-
3I will have a set up file. I want to give to a user option to delet it after sucsessfull run. – Rella Apr 05 '10 at 15:07
-
2i use it to delete autologin script – vladkras Apr 25 '16 at 09:36
4 Answers
15
Here's a great way of ensuring the script gets deleted, no matter if intervening code calls exit() or not.
class DeleteOnExit
{
function __destruct()
{
unlink(__FILE__);
}
}
$g_delete_on_exit = new DeleteOnExit();

Ben
- 151
- 1
- 2
0
unlink() is the valid function for this, but sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances.
class SelfDelete{
public static $obj;
function __destruct(){
unlink(__FILE__);
}
function _self(){
self::$obj = new SelfDelete();
}
}
Auth::_self();

Murad Hasan
- 9,565
- 2
- 21
- 42
0
If you can't use unlink() try create .htaccess
<Files "install.php">
Deny from all
</Files>

Takahata Shun
- 1
- 1