如何让 php 脚本删除自身(包括目录) [英] How to make php script delete itself (and includes dir)

查看:58
本文介绍了如何让 php 脚本删除自身(包括目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让脚本在完成工作后自行删除?

How do I make script delete itself after it'll finish its work?

这是我的安装脚本,出于安全原因,我希望它自行删除(因此攻击者将无法覆盖现有站点).

It's for my installation script, I want it to delete itself for security reasons (so attacker won't be able to overwrite existing site).

我忘了提到它有它的'includes'目录,我也想删除它.​​.. 有人可以添加如何也删除这个目录吗?包含目录是安装脚本所在文件夹的子目录.

I forgot to mention that it has its 'includes' directory that i would like to be deleted too... Could someone add how to also delete this dir? Includes directory is subdirectory of the same folder where install script is located.

推荐答案

您可以使用 unlink 删除文件,__FILE__ 获取当前文件的完整路径:

You can use unlink to remove a file, and __FILE__ to get the full path to the current file :

unlink(__FILE__);

作为证据":

squale@shark:~/developpement/tests/temp
$ ll | grep 'remove-myself.php'
-rw-r--r-- 1 squale   squale      25 2009-08-01 17:01 remove-myself.php

=> 文件存在

squale@shark:~/developpement/tests/temp
$ cat remove-myself.php
<?php

unlink(__FILE__);

=> 里面包含我给的代码

=> It contains the code I gave

squale@shark:~/developpement/tests/temp
$ php ./remove-myself.php

=> 我启动脚本

squale@shark:~/developpement/tests/temp
$ ll | grep 'remove-myself.php'

=> 已经不存在了


为此,您必须确保拥有所需的权限……这意味着尝试删除文件的用户需要对包含该文件的目录具有正确的访问权限.


For this to work, you'll have to be sure you have the required privilegies... this means the user trying to delete the file needs to have right-access on the directory containing it.

当你在命令行中时,一般没问题;但是,如果您尝试通过 Apache 执行此操作,则需要授予 Apache 对该目录/文件的写访问权限——默认情况下,Apache 通常没有这种权限(不安全,通常不需要)

When you are in command line, it's generally OK ; but if you are trying to do this via Apache, you will need to give Apache write-access to that directory/file -- Apache doesn't generally have that kind of privilege by default (not secure, and generally not needed)


不确定它是否可以在 Windows 上使用,不过……它可以在 Linux 上运行,但 Windows 在执行文件时可能会有点锁定"文件……

这篇关于如何让 php 脚本删除自身(包括目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆