gpt4 book ai didi

php - 使用 array_slice 删除 scandir 点和点-点条目是否安全?

转载 作者:可可西里 更新时间:2023-11-01 01:01:16 25 4
gpt4 key购买 nike

我想在我的 PHP 脚本中使用 array_slicescandir

正常使用:

<?php

$files = scandir('/path/to/files');

foreach($files as $file) {
if($file != '.' && $file != '..') {
// Do something here...
}
}

我的例子:

<?php

$files = array_slice(scandir('/path/to/files'), 2);

foreach($files as $file) {
// Do something here...
}

我的疑问是,使用这种逻辑是否安全?

最佳答案

绝对不安全。以下示例创建一个目录,其中包含一个名为 ! 的文件。 scandir对结果进行排序时,!出现在...之前:

mkdir('test');
touch('test/!');
print_r(scandir('test'));
unlink('test/!');
rmdir('test');

输出:

Array
(
[0] => !
[1] => .
[2] => ..
)

一般来说,这对于所有以 . 之前的字符开头的文件名都是一个问题。这包括一些不可打印的字符,这些字符可能不会存在于现实世界的数据中,但它也适用于常见的标点符号,包括 ! # $ % & ( ) + -.

即使它有效,我也不推荐它,因为在那里使用 array_slice 会使代码的意图不那么清晰。

关于php - 使用 array_slice 删除 scandir 点和点-点条目是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25349092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com