douhao2011 2014-11-05 22:59
浏览 63
已采纳

PHP文件重命名自己

I'm attempting to write an upgrade script for one of our website. Currently the script will unzip files provided from the upgrade link and unzip to the directory.

My problem is if there is a new version of itself i.e. upgrader-new.php I need the script to run to completion and then delete itself, then haveupgrader-new.php be renamed to upgrader.php.

The only solution I can think of would be to create a third file i.e. upgrade-assist.php that would be run before any upgrade to check if upgrader-new.php exists and if it does, delete the original and rename the new to the proper name. I forsee problems because the original script would still have to call upgrader-assist.php and as far as I know you can't delete a running script?

Any suggestions?

Thanks!

  • 写回答

1条回答 默认 最新

  • dony39517 2014-11-05 23:05
    关注

    as far as I know you can't delete a running script?

    You can, try this one:

    <?php unlink(__FILE__);
    

    It is perfectly fine, unless there are other processes using this file (and of course running process has enough permission to do so).

    File is not processed and executed line by line, but loaded into memory so you can freely rename or delete it, and it will still run until the end (or fatal error).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?