php获取文件夹下面的文件列表和文件夹列表

function getDir($dir) {
  $dirArray[] = NULL;
  if (false != ($handle = opendir( $dir ))) {
    $i=0;
    while ( false !== ($file = readdir( $handle )) ) {
      //去掉"“.”、“..”以及带“.xxx”后缀的文件
      if ($file != "." && $file != ".."&&!strpos($file,".")) {
        $dirArray[$i] = $file;
        $i++;
      }
    }
    //关闭句柄
    closedir ($handle);
  }
  return $dirArray;
}

//获取文件列表
function getFile($dir) {
  $fileArray[] = NULL;
  if (false != ($handle = opendir ( $dir ))) {
    $i=0;
    while ( false !== ($file = readdir ( $handle )) ) { 
      if ($file != "." && $file != ".."&&strpos($file,".")) {
        $fileArray[$i]['url'] = $dir.$file;
        $fileArray[$i]['name'] = $file;
        if($i==100){
          break;
        }
      $i++;
      }
    }
    //关闭句柄
    closedir ($handle);
  }
  return $fileArray;
}




$dir = "D:/.......";    //绝对路径
$data = getFile($dir);  //获取文件列表
echo "<pre>";
print_r($data);

  

 

原文地址:https://www.cnblogs.com/qhorse/p/5144469.html