如何获得scandir()的完整路径

时间:2017-09-11 20:33:43

标签: php arrays pocketmine

当我scandir()时它会像这样返回数组

Array(
  [0]=>
  string(8) "DevTools"
  [1]=>
  string(17) "LB-Particles.phar"
  [2]=>
  string(11) "NoGrifersDP"
  [3]=>
  string(12) "PacketLogger"
  [4]=>
  string(24) "PocketMine-DevTools.phar"
)

所以我想让它获得像这样的文件的完整路径

Array(
  [0]=>
  string(8) "C:\Users\USERNAME\PocketMine-MP\plugins\DevTools"
  [1]=>
  string(17) "C:\Users\USERNAME\PocketMine-MP\plugins\LB-Particles.phar"
  [2]=>
  string(11) "C:\Users\USERNAME\PocketMine-MP\plugins\NoGrifersDP"
  [3]=>
  string(12) "C:\Users\USERNAME\PocketMine-MP\plugins\PacketLogger"
  [4]=>
  string(24) "C:\Users\USERNAME\PocketMine-MP\plugins\PocketMine-DevTools.phar"
)

我尝试使用但不起作用的代码

$pluginsfolder = $this->getServer()->getDataPath()."plugins/";
$plugins = array_slice(scandir($pluginsfolder), 2);
foreach ($plugins as $plugin);
$pluginspath = $pluginsfolder . $plugin;
var_dump($pluginspath);

1 个答案:

答案 0 :(得分:0)

您可以使用引用&修改每个元素:

foreach($plugins as &$plugin) {
    $plugin = $pluginsfolder . $plugin;
}

或者用钥匙:

foreach($plugins as $key => $plugin) {
    $plugins[$key] = $pluginsfolder . $plugin;
}

或没有foreach

$plugins = array_map(function($plugin) use($pluginsfolder) {
                         return $pluginsfolder.$plugin
                     }, $plugins);

但是,glob()将返回完整路径:

$plugins = glob("$pluginsfolder*")