PHP file that should run once and delete itself. I

2019-02-02 23:45发布

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

3条回答
时光不老,我们不散
2楼-- · 2019-02-03 00:15

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();
查看更多
虎瘦雄心在
3楼-- · 2019-02-03 00:18
<?php unlink(__FILE__); ?>
查看更多
Evening l夕情丶
4楼-- · 2019-02-03 00:29

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();
查看更多
登录 后发表回答