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