PHP file that should run once and delete itself. Is it possible?
Asked Answered
B

4

28

Is it possible to create a PHP file that runs once with no errors and deletes itself?

Boneset answered 5/4, 2010 at 14:57 Comment(3)
Out of interest, why do you wish to do this?Leicester
I will have a set up file. I want to give to a user option to delet it after sucsessfull run.Boneset
i use it to delete autologin scriptOleaceous
N
48
<?php unlink(__FILE__); ?>
Nonchalant answered 5/4, 2010 at 15:4 Comment(0)
N
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();
Nasturtium answered 24/3, 2011 at 16:44 Comment(0)
F
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();
Frazzle answered 21/7, 2016 at 7:35 Comment(1)
How does this answer differ from the one posted by Ben?Pomona
D
0

If you can't use unlink() try create .htaccess

<Files "install.php">  
  Deny from all
</Files>
Durston answered 31/1, 2020 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.