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

為{幸葍}努か 提交于 2019-11-30 12:49:05

问题


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


回答1:


<?php unlink(__FILE__); ?>



回答2:


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();



回答3:


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();


来源:https://stackoverflow.com/questions/2579017/php-file-that-should-run-once-and-delete-itself-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!