duanchuanqu593743 2017-06-23 10:53
浏览 43
已采纳

使用PHP从文件和目录数组中删除文件

I'm trying to get a list of directories in a path by using scandir and removing the files from the array but I'm getting errors from my echo.

I'm using the code

$path = "Calltypes/";
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));

for($i = 0; $i < sizeof($files); $i++) {
    if(!is_dir($files[$i]))
        array_splice($files, $i, 1);
}

echo sizeof($files);

This is the echo output:

Notice: Undefined offset: 0 in C:\xampp\htdocs\index.php on line 32
2

The Calltypes/ path has 3 folders and 1 txt file.

Edit: line 32 is

if(!is_dir($files[$i]))
  • 写回答

2条回答 默认 最新

  • dougou8552 2017-06-23 11:01
    关注

    You can use foreach instead of for. After that use unset because array_splice will rearrange keys so you will get errors

    $path = "Calltypes/";
    $files = scandir($path);
    $files = array_diff(scandir($path), array('.', '..'));
    
    foreach ($files as $key => $value) {
        if(!is_dir($path.$files[$key]))
           unset($files[$key]);
    }
    echo sizeof($files);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器