使用通配符删除文件-exec与unlink [英] Delete files using wildcard - exec vs unlink

查看:67
本文介绍了使用通配符删除文件-exec与unlink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个php脚本,在这里我想使用通配符(*)从给定的文件夹中删除一些文件.
我找到了一些可行的示例,例如这一个,其中使用 unlink() glob()函数.

I'm working on a php script, where I want to delete some files from a given folder using wildcard (*).
I've found some working examples like this one, where unlink() and glob() function are used.

现在,我想知道,使用 exec 函数和 rm -f/path/to/folder/_prefix _ * 这样的命令删除文件是否还可以?代码>?
使用此工具是否有任何安全隐患?
如果可以的话,在性能方面会更好吗?

Now, i was wondering, would it also be ok to delete the files using the exec function and a command like rm -f /path/to/folder/_prefix_* ?
Are there any security risks taken using this?
And if it is ok, would it be better in terms of performance?


因此,从第一个答案中,我确实可以看出,使用 exec 是可以接受的解决方案.
那么性能问题呢? exec 选项是否有可能比 glob/unlink 技术更好(更快/要求更低)?


So, from the first answers i can see that indeed, using exec could be an acceptable solution.
What about performance issues? Is there any chance the exec option could be better (faster/less demanding) over the glob/unlink technique?

提前谢谢

推荐答案

由于没有机会注入用户提供的数据,因此在上使用 exec 时没有安全问题glob/unlink .但是,使用 glob/unlink 可以定义异常:

Because there is no chance for user-supplied data to be injected, there is no security issue in using exec over glob/unlink. However, using glob/unlink allows you to define exceptions:

foreach(glob("delete/*") as $f) {
    if( $f == "delete/notme.txt") continue;
    unlink($f);
}

exec 通常在共享服务器上被禁用,因此 glob/unlink 更具可移植性.如果您有专用的设置并且不打算放弃它,则不必担心.

And exec is often disabled on shared servers so glob/unlink is more portable. If you have a dedicated setup and don't intend on giving it up, you don't need to worry about that.

这篇关于使用通配符删除文件-exec与unlink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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